Friday, July 31, 2009

Qt4 Imap API

Pushed in my GitHub repo the first draft of the Qt4 Imap (RFC 3501 Interne Message Access Protocol) Library.
I've implemented a sync communication because it's much easier to use, so if you want to use it in async way you need to implement a thread class that wrap the Imap class and throws the events that you need.


Imap imap;
if (!imap.connectToHost(IMAP_HOST, IMAP_PORT, IMAP_USE_SSL))
IMAP_MAIN_ABORT("connectToHost()", imap.errorString());

if (!imap.login(IMAP_USERNAME, IMAP_PASSWORD, IMAP_LOGIN_TYPE))
IMAP_MAIN_ABORT("login()", imap.errorString());

ImapMailbox *mailbox = imap.select("INBOX");
qDebug() << "INBOX";
qDebug() << " - Exists:" << mailbox->exists();
qDebug() << " - Unseen:" << mailbox->unseen();

QList<int> messageList = imap.searchRecentUnseen();
imap.fetch(mailbox, messageList);
foreach (int msgId, messageList) {
ImapMessage *message = mailbox->findById(msgId);
if (message == NULL) continue;

imap.fetchBodyStructure(message);

qDebug() << "FROM" << message->fromAddress().toString();
foreach (ImapAddress address, message->toAddresses())
qDebug() << " - TO" << address.toString();
qDebug() << "SUBJECT" << message->subject();

for (int i = 0; i < message->bodyPartCount(); ++i) {
ImapMessageBodyPart *bodyPart = message->bodyPartAt(i);
imap.fetchBodyPart(message, i);

qDebug() << bodyPart->isAttachment() << bodyPart->contentType();
qDebug() << bodyPart->data();
}
}

delete mailbox;

imap.logout();
imap.disconnectFromHost();

Thursday, July 23, 2009

Qt4 Google Contacts APIs

Just pushed at GitHub the first draft of the Qt4 Google Contacts Service...

Contacts APIs allows client applications to view and update Contacts content in the form of Google Data API feeds. Your client application can request a list of a user's contacts, edit or delete content in an existing contact.


// Setup Contact
THGoogleContact *contact = new THGoogleContact;
contact->setFullName("John Doe");

THGoogleIm *im = new THGoogleIm;
im->setRel(THGoogleIm::relWork());
im->setAddress("john.doe@aim.com");
im->setProtocol(THGoogleIm::protocolAim());
contact->addImAddress(im);

THGoogleEmail *email = new THGoogleEmail;
email->setAddress("john.doe@mail.com");
email->setRel(THGoogleEmail::relWork());
contact->addEmailAddress(email);

// Insert Contact
THGoogleContactsService *gContacts = new THGoogleContactsService;
gContacts->setAuthToken(...);
gContacts->insertContact(contact);


Take a look at contacttest.cpp for a more complete usage example.

Tuesday, July 14, 2009

OpenGL: 3D Cube, Rotation, Translation and Texts

Second hour with OpenGL, last time I've written a simple 2D example to learn what Vertex are, and now a little step forward using 3D, Rotation, Translation and Texts.

Using GLUT writing 2D texts is really simple, and here is how to do it. Setup Text Color, X and Y Location, pick up a font and set your string, and it's done.


glColor3f(1.0, 0.0, 0.0);
glRasterPos2f(-4.6, 2.3);
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, 'Hello Text');


To handle left, right, up, down keys to move the cube use the glutSpecialFunc() that allows you to handle GLUT_KEY_LEFT, GLUT_KEY_RIGHT, GLUT_KEY_UP, GLUT_KEY_DOWN keys and others. Translation and rotation are made using glTranslatef() and glRotatef().

The source code is available here: Python OpenGL 3D Cube Source Code.

Sunday, July 12, 2009

OpenGL: First Test - 2D Example

I'm not a Computer Graphics fan, but I've spent my the last year working on Report Engine, and User Interfaces... drawLine, drawRect.. setLocation... (I don't know.. I'm a malloc/memset boy).

But I need to learn more about Computer Graphics for the future, and today I've started playing with OpenGL... No more 2D points!!! :D
Below you can see a couple of lines of code that draws the example above...



static void drawTriangle (void) {
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);

glVertex3f( 0.0f,  0.6f, 0.0f);
glVertex3f(-0.2f, -0.3f, 0.0f);
glVertex3f( 0.2f, -0.3f, 0.0f);

glEnd();
}

static void drawSquare (void) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLE_FAN);

glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.5f, 0.5f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);

glEnd();
}

static void drawSquareMode2 (void) {
const GLfloat squareVertices[] = {
-0.2,  0.2, 0.2, 
-0.2, -0.2, 0.2,
0.2, -0.2, 0.2, 
0.2,  0.2, 0.2 
};

glColor3f(0.0f, 1.0f, 0.0f);
glVertexPointer(3, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}


I've used NSOpenGLView to display the GL, so the drawing code is inside a drawRect method.
- (void)drawRect:(NSRect)bounds {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);

glTranslatef(-0.2f, 0.0f, 0.0f);
drawTriangle();

glTranslatef(0.2f, 0.0f, 0.0f);
drawSquare();
drawSquareMode2();

glFlush();
}