From: <svn...@op...> - 2009-09-26 14:16:54
|
Author: paule Date: Sat Sep 26 15:37:36 2009 New Revision: 5853 URL: http://www.opensync.org/changeset/5853 Log: opie-sync: fix code indenting style Modified: plugins/opie-sync/src/opie_qcop.c plugins/opie-sync/src/opie_qcop.h Modified: plugins/opie-sync/src/opie_qcop.c ============================================================================== --- plugins/opie-sync/src/opie_qcop.c Fri Sep 25 21:44:39 2009 (r5852) +++ plugins/opie-sync/src/opie_qcop.c Sat Sep 26 15:37:36 2009 (r5853) @@ -58,84 +58,84 @@ gboolean expect(qcop_conn* qconn, char* str, char *failstr, char* errmsg); /* globals */ -pthread_t monitor_thd = 0; +pthread_t monitor_thd = 0; - + /* get_line - * + * * helper function to receive a all characters up to the next newline from the socket * caller is responsible for g_free()ing the returned string resp. for checking that * a string was returned at all. */ gchar* get_line(qcop_conn* qconn) { - GString* buffer; - char* retval = NULL; - gchar* curr_char; - ssize_t received; - - buffer = g_string_new(""); - curr_char = g_strndup("",1); - while ((received = read(qconn->socket, curr_char, 1)) && !strstr(curr_char, "\n")) - { - if(received < 0) - { - /* TODO - recv failed - log */ - - break; - } - else - g_string_append(buffer,curr_char); - } - - if(buffer->str && (buffer->len > 0)) - { - retval = g_strdup(buffer->str); - g_free(curr_char); - g_string_free(buffer,TRUE); - } - else retval = NULL; - - /* caller must g_free() retval */ - return(retval); + GString* buffer; + char* retval = NULL; + gchar* curr_char; + ssize_t received; + + buffer = g_string_new(""); + curr_char = g_strndup("",1); + while ((received = read(qconn->socket, curr_char, 1)) && !strstr(curr_char, "\n")) + { + if(received < 0) + { + /* TODO - recv failed - log */ + + break; + } + else + g_string_append(buffer,curr_char); + } + + if(buffer->str && (buffer->len > 0)) + { + retval = g_strdup(buffer->str); + g_free(curr_char); + g_string_free(buffer,TRUE); + } + else retval = NULL; + + /* caller must g_free() retval */ + return(retval); } /* send_allof * * helper function to send strings of arbitrary length to socket - * returns: + * returns: */ gboolean send_allof(qcop_conn* qconn, char* str) { - int len,bytes_sent; - char* msg; - gboolean rc = TRUE; - - if(str) - { - msg = g_strdup(str); - bytes_sent = 0; - len = strlen(str); - while (bytes_sent < len) - { - bytes_sent = send(qconn->socket, str, (len - bytes_sent), 0); - if(bytes_sent < 0) - { - /* TODO - error log */ - rc = FALSE; - break; - } - else - { - str = str + bytes_sent; - len = strlen(str); - } - } - g_free(msg); - } - - return rc; + int len,bytes_sent; + char* msg; + gboolean rc = TRUE; + + if(str) + { + msg = g_strdup(str); + bytes_sent = 0; + len = strlen(str); + while (bytes_sent < len) + { + bytes_sent = send(qconn->socket, str, (len - bytes_sent), 0); + if(bytes_sent < 0) + { + /* TODO - error log */ + rc = FALSE; + break; + } + else + { + str = str + bytes_sent; + len = strlen(str); + } + } + g_free(msg); + } + + return rc; } /* @@ -150,41 +150,41 @@ gboolean expect(qcop_conn* qconn, char* str, char* failstr, char* errmsg) { - char* pc; + char* pc; - pc = get_line(qconn); - if (pc) /* did we get anything at all? */ - { - if (strstr(pc,str)) /* wait for desired substring */ - { - g_free(pc); - return TRUE; - } - else - { - if (failstr && (strlen(failstr) > 0) && (strstr(pc, failstr))) { - qconn->resultmsg = g_strdup(errmsg); - return FALSE; - } - - if (strstr(pc,"cancelSync")) - { - g_free(pc); - qconn->resultmsg = g_strdup("User cancelled sync"); - return FALSE; - } - else - { - g_free(pc); - return expect(qconn, str, failstr, errmsg); - } - } - } - else - { - qconn->resultmsg = g_strdup(errmsg); - return FALSE; - } + pc = get_line(qconn); + if (pc) /* did we get anything at all? */ + { + if (strstr(pc,str)) /* wait for desired substring */ + { + g_free(pc); + return TRUE; + } + else + { + if (failstr && (strlen(failstr) > 0) && (strstr(pc, failstr))) { + qconn->resultmsg = g_strdup(errmsg); + return FALSE; + } + + if (strstr(pc,"cancelSync")) + { + g_free(pc); + qconn->resultmsg = g_strdup("User cancelled sync"); + return FALSE; + } + else + { + g_free(pc); + return expect(qconn, str, failstr, errmsg); + } + } + } + else + { + qconn->resultmsg = g_strdup(errmsg); + return FALSE; + } } /* Waits for a line from the qcop server that must match one of the substrings @@ -193,44 +193,44 @@ */ gboolean expect_special(qcop_conn* qconn, char* errmsg, gboolean flushing) { - char* pc; + char* pc; - pc = get_line(qconn); - if (pc) - { - if (strstr(pc,"599") || strstr(pc,"200")) - { - if(strstr(pc,"200") && flushing) - { - g_free(pc); - return expect(qconn, "flushDone", NULL, errmsg); - } - else - { - g_free(pc); - return TRUE; - } - } - else - { - if (strstr(pc,"cancelSync")) - { - g_free(pc); - qconn->resultmsg = g_strdup("User cancelled sync"); - return FALSE; - } - else - { - g_free(pc); - return expect_special(qconn, errmsg, flushing); - } - } - } - else - { - qconn->resultmsg = g_strdup(errmsg); - return FALSE; - } + pc = get_line(qconn); + if (pc) + { + if (strstr(pc,"599") || strstr(pc,"200")) + { + if(strstr(pc,"200") && flushing) + { + g_free(pc); + return expect(qconn, "flushDone", NULL, errmsg); + } + else + { + g_free(pc); + return TRUE; + } + } + else + { + if (strstr(pc,"cancelSync")) + { + g_free(pc); + qconn->resultmsg = g_strdup("User cancelled sync"); + return FALSE; + } + else + { + g_free(pc); + return expect_special(qconn, errmsg, flushing); + } + } + } + else + { + qconn->resultmsg = g_strdup(errmsg); + return FALSE; + } } /* @@ -240,56 +240,56 @@ */ qcop_conn* qcop_connect(gchar* addr, gchar* username, gchar* password) { - struct sockaddr_in host_addr; - qcop_conn* qconn = NULL; + struct sockaddr_in host_addr; + qcop_conn* qconn = NULL; - host_addr.sin_family = AF_INET; /* host byte order */ - host_addr.sin_port = htons(OPIEQCOP_PORT); /* short, network byte order */ - host_addr.sin_addr.s_addr = inet_addr(addr); - memset(&(host_addr.sin_zero), '\0', 8); /* zero the rest of the struct */ - - qconn = g_malloc0(sizeof(qcop_conn)); - - qconn->socket = socket(AF_INET, SOCK_STREAM, 0); - qconn->result = FALSE; /* Only set to TRUE if successful */ - if(qconn->socket < 0) - { - qconn->resultmsg = g_strdup_printf("Could not create socket: %s", strerror(errno)); - return qconn; - } - - /* connect to the QCopBridgeServer */ - if (connect(qconn->socket, (struct sockaddr *)&host_addr, sizeof(host_addr)) != -1) - { - - if (expect(qconn, "220", NULL, "Failed to log into server - please check sync security settings on device")) - { - send_allof(qconn, "USER "); - send_allof(qconn, username); - send_allof(qconn, "\n"); - } - else - return qconn; - - if(expect(qconn, "331", "530", "Failed to log into server - please check username")) - { - send_allof(qconn, "PASS "); - send_allof(qconn, password); - send_allof(qconn, "\n"); - } - else - return qconn; - - if(!expect(qconn, "230", "530", "Failed to log into server - please check username / password")) - return qconn; - - /* connected OK */ - qconn->result = TRUE; - } - else - qconn->resultmsg = g_strdup_printf("Could not connect to server: %s", strerror(errno)); - - return qconn; + host_addr.sin_family = AF_INET; /* host byte order */ + host_addr.sin_port = htons(OPIEQCOP_PORT); /* short, network byte order */ + host_addr.sin_addr.s_addr = inet_addr(addr); + memset(&(host_addr.sin_zero), '\0', 8); /* zero the rest of the struct */ + + qconn = g_malloc0(sizeof(qcop_conn)); + + qconn->socket = socket(AF_INET, SOCK_STREAM, 0); + qconn->result = FALSE; /* Only set to TRUE if successful */ + if(qconn->socket < 0) + { + qconn->resultmsg = g_strdup_printf("Could not create socket: %s", strerror(errno)); + return qconn; + } + + /* connect to the QCopBridgeServer */ + if (connect(qconn->socket, (struct sockaddr *)&host_addr, sizeof(host_addr)) != -1) + { + + if (expect(qconn, "220", NULL, "Failed to log into server - please check sync security settings on device")) + { + send_allof(qconn, "USER "); + send_allof(qconn, username); + send_allof(qconn, "\n"); + } + else + return qconn; + + if(expect(qconn, "331", "530", "Failed to log into server - please check username")) + { + send_allof(qconn, "PASS "); + send_allof(qconn, password); + send_allof(qconn, "\n"); + } + else + return qconn; + + if(!expect(qconn, "230", "530", "Failed to log into server - please check username / password")) + return qconn; + + /* connected OK */ + qconn->result = TRUE; + } + else + qconn->resultmsg = g_strdup_printf("Could not connect to server: %s", strerror(errno)); + + return qconn; } @@ -298,16 +298,16 @@ */ void qcop_disconnect(qcop_conn* qconn) { - if(qconn) - { - if(qconn->socket) - { - send_allof(qconn, "QUIT\n"); - close(qconn->socket); - } - - qcop_freeqconn(qconn); - } + if(qconn) + { + if(qconn->socket) + { + send_allof(qconn, "QUIT\n"); + close(qconn->socket); + } + + qcop_freeqconn(qconn); + } } @@ -316,12 +316,12 @@ */ void qcop_freeqconn(qcop_conn* qconn) { - if (qconn) - { - if (qconn->resultmsg) - g_free(qconn->resultmsg); - g_free(qconn); - } + if (qconn) + { + if (qconn->resultmsg) + g_free(qconn->resultmsg); + g_free(qconn); + } } @@ -330,59 +330,59 @@ */ char* qcop_get_root(qcop_conn* qconn) { - gchar* temp = NULL; - char* start; - gchar* pc; - - send_allof(qconn, "CALL QPE/System sendHandshakeInfo()\n"); - - if (!expect(qconn, "200", NULL, "Failed to obtain HandshakeInfo")) - return NULL; - pc = get_line(qconn); - if(!strstr(pc, "handshakeInfo(QString,bool)")) - { - qconn->resultmsg = g_strdup_printf("Unrecognised response: %s", pc); - g_free(pc); - return NULL; - } - - if ((start=strstr(strstr(pc,"/")+1,"/"))) /* We need the second slash */ - { - /* caller responsible for free()ing temp */ - temp = g_strndup(start,strstr(start," ")-start); /* from slash to blank is our path */ - } - else if((start=strstr(pc,") ")+2)) - { - /* Qtopia sends back a base64 encoded utf-16 (big-endian) string */ - guchar *decoded; - char *startc; - gsize len = 0; - gsize len2 = 0; - GError *err = NULL; - - decoded = g_base64_decode(start, &len); - if(len > 0) { - /* first four bytes seem to be \0 \0 \0 (string length) */ - len = decoded[3]; - startc = decoded + 4; - temp = g_convert(startc, len, "UTF8", "UTF16BE", NULL, &len2, &err); - if (err != NULL) { - fprintf(stderr, "UTF16 convert error: %s\n", err->message); - g_error_free(err); - if(temp) { - /* Don't accept partial conversions */ - g_free(temp); - temp = NULL; - } - } - } - } + gchar* temp = NULL; + char* start; + gchar* pc; + + send_allof(qconn, "CALL QPE/System sendHandshakeInfo()\n"); + + if (!expect(qconn, "200", NULL, "Failed to obtain HandshakeInfo")) + return NULL; + pc = get_line(qconn); + if(!strstr(pc, "handshakeInfo(QString,bool)")) + { + qconn->resultmsg = g_strdup_printf("Unrecognised response: %s", pc); + g_free(pc); + return NULL; + } + + if ((start=strstr(strstr(pc,"/")+1,"/"))) /* We need the second slash */ + { + /* caller responsible for free()ing temp */ + temp = g_strndup(start,strstr(start," ")-start); /* from slash to blank is our path */ + } + else if((start=strstr(pc,") ")+2)) + { + /* Qtopia sends back a base64 encoded utf-16 (big-endian) string */ + guchar *decoded; + char *startc; + gsize len = 0; + gsize len2 = 0; + GError *err = NULL; + + decoded = g_base64_decode(start, &len); + if(len > 0) { + /* first four bytes seem to be \0 \0 \0 (string length) */ + len = decoded[3]; + startc = decoded + 4; + temp = g_convert(startc, len, "UTF8", "UTF16BE", NULL, &len2, &err); + if (err != NULL) { + fprintf(stderr, "UTF16 convert error: %s\n", err->message); + g_error_free(err); + if(temp) { + /* Don't accept partial conversions */ + g_free(temp); + temp = NULL; + } + } + } + } - if(!temp) - qconn->resultmsg = g_strdup_printf("Unrecognised response: %s", pc); + if(!temp) + qconn->resultmsg = g_strdup_printf("Unrecognised response: %s", pc); - g_free(pc); - return temp; + g_free(pc); + return temp; } @@ -391,48 +391,48 @@ */ void qcop_start_sync(qcop_conn* qconn, void (*cancel_routine)()) { - qcop_monitor_data* data; + qcop_monitor_data* data; - qconn->result = FALSE; /* ..until proven otherwise */ + qconn->result = FALSE; /* ..until proven otherwise */ - /* first lock the UI */ - send_allof(qconn, "CALL QPE/System startSync(QString) OpenSync\n"); - if(!expect(qconn, "200", NULL, "Failed to bring up sync screen!")) - return; - /* Flush addressbook to storage */ - send_allof(qconn, "CALL QPE/Application/addressbook flush()\n"); - if (!expect_special(qconn,"Failed to flush addressbook",TRUE)) - return; - /* Flush datebook to storage */ - send_allof(qconn, "CALL QPE/Application/datebook flush()\n"); - if (!expect_special(qconn,"Failed to flush datebook",TRUE)) - return; - /* Flush todolist to storage */ - send_allof(qconn, "CALL QPE/Application/todolist flush()\n"); - if (!expect_special(qconn,"Failed to flush todolist",TRUE)) - return; - - /* spawn the monitor thread */ - data = g_malloc0(sizeof(qcop_monitor_data)); - data->qconn = qconn; - data->cancel_routine = cancel_routine; - qconn->syncing = TRUE; - pthread_mutex_init(&qconn->access_mutex,NULL); - /* FIXME the monitor thread interferes with reading data from - the socket in the main thread (in a non-deterministic way - even!); since we don't support cancelling at the moment anyway - I have disabled the monitor thread until I can figure out the - proper way of doing this. */ -/* if(pthread_create(&monitor_thd, - NULL, - (void *)monitor_thread_main, - (void*)data) != 0) - {*/ - /* error creating thread */ - /* TODO - log */ -/* }*/ - qconn->result = TRUE; - return; + /* first lock the UI */ + send_allof(qconn, "CALL QPE/System startSync(QString) OpenSync\n"); + if(!expect(qconn, "200", NULL, "Failed to bring up sync screen!")) + return; + /* Flush addressbook to storage */ + send_allof(qconn, "CALL QPE/Application/addressbook flush()\n"); + if (!expect_special(qconn,"Failed to flush addressbook",TRUE)) + return; + /* Flush datebook to storage */ + send_allof(qconn, "CALL QPE/Application/datebook flush()\n"); + if (!expect_special(qconn,"Failed to flush datebook",TRUE)) + return; + /* Flush todolist to storage */ + send_allof(qconn, "CALL QPE/Application/todolist flush()\n"); + if (!expect_special(qconn,"Failed to flush todolist",TRUE)) + return; + + /* spawn the monitor thread */ + data = g_malloc0(sizeof(qcop_monitor_data)); + data->qconn = qconn; + data->cancel_routine = cancel_routine; + qconn->syncing = TRUE; + pthread_mutex_init(&qconn->access_mutex,NULL); + /* FIXME the monitor thread interferes with reading data from + the socket in the main thread (in a non-deterministic way + even!); since we don't support cancelling at the moment anyway + I have disabled the monitor thread until I can figure out the + proper way of doing this. */ +/* if(pthread_create(&monitor_thd, + NULL, + (void *)monitor_thread_main, + (void*)data) != 0) + {*/ + /* error creating thread */ + /* TODO - log */ +/* }*/ + qconn->result = TRUE; + return; } @@ -441,35 +441,35 @@ */ void qcop_stop_sync(qcop_conn* qconn) { - if(qconn && qconn->syncing) - { - pthread_mutex_lock(&qconn->access_mutex); - qconn->result = FALSE; /* ..until... */ - qconn->syncing = FALSE; - pthread_mutex_unlock(&qconn->access_mutex); - if (monitor_thd) - pthread_join(monitor_thd,NULL); - pthread_mutex_destroy(&qconn->access_mutex); - /* Reload addressbook data */ - send_allof(qconn, "CALL QPE/Application/addressbook reload()\n"); - if(!expect_special(qconn,"Failed to reload addressbook",FALSE)) - return; - /* Reload datebook data */ - send_allof(qconn, "CALL QPE/Application/datebook reload()\n"); - if(!expect_special(qconn,"Failed to reload datebook",FALSE)) - return; - /* Reload todolist data */ - send_allof(qconn, "CALL QPE/Application/todolist reload()\n"); - if(!expect_special(qconn,"Failed to reload todolist",FALSE)) - return; - /* unlock the GUI */ - send_allof(qconn, "CALL QPE/System stopSync()\n"); - if(!expect(qconn, "200", NULL, "Failed to close sync screen")) - return; - qconn->result = TRUE; - } - - return; + if(qconn && qconn->syncing) + { + pthread_mutex_lock(&qconn->access_mutex); + qconn->result = FALSE; /* ..until... */ + qconn->syncing = FALSE; + pthread_mutex_unlock(&qconn->access_mutex); + if (monitor_thd) + pthread_join(monitor_thd,NULL); + pthread_mutex_destroy(&qconn->access_mutex); + /* Reload addressbook data */ + send_allof(qconn, "CALL QPE/Application/addressbook reload()\n"); + if(!expect_special(qconn,"Failed to reload addressbook",FALSE)) + return; + /* Reload datebook data */ + send_allof(qconn, "CALL QPE/Application/datebook reload()\n"); + if(!expect_special(qconn,"Failed to reload datebook",FALSE)) + return; + /* Reload todolist data */ + send_allof(qconn, "CALL QPE/Application/todolist reload()\n"); + if(!expect_special(qconn,"Failed to reload todolist",FALSE)) + return; + /* unlock the GUI */ + send_allof(qconn, "CALL QPE/System stopSync()\n"); + if(!expect(qconn, "200", NULL, "Failed to close sync screen")) + return; + qconn->result = TRUE; + } + + return; } @@ -478,57 +478,57 @@ */ void monitor_thread_main(qcop_monitor_data* data) { - /* listen on the socket and read the input, looking for the cancel - * when the cancel is found, call the supplied cancel routine - */ - fd_set qcop_socket; - struct timeval tv; - int retval; - gchar* mon_pc; - - FD_ZERO(&qcop_socket); - FD_SET(data->qconn->socket,&qcop_socket); - - while(1) - { - /* qconn is ours for the time being */ - pthread_mutex_lock(&data->qconn->access_mutex); - - /* wait for one second with each select() */ - tv.tv_sec = 1; - tv.tv_usec = 0; - - retval=select(data->qconn->socket+1,&qcop_socket,NULL,NULL,&tv); - if (retval > 0) - { - mon_pc = get_line(data->qconn); - if ( mon_pc ) - { - if (strstr(mon_pc,"cancelSync()")) - { - g_free(mon_pc); - data->qconn->syncing = FALSE; - (data->cancel_routine)(); - } - else - { - perror("Error on select() call or no data"); - g_free(mon_pc); - } - } - } - - /* we are done */ - if (!data->qconn->syncing) - { - pthread_mutex_unlock(&data->qconn->access_mutex); - g_free(data); - pthread_exit(NULL); - } - - /* unlock for a moment allow access for other threads */ - pthread_mutex_unlock(&data->qconn->access_mutex); - sleep(1); - } + /* listen on the socket and read the input, looking for the cancel + * when the cancel is found, call the supplied cancel routine + */ + fd_set qcop_socket; + struct timeval tv; + int retval; + gchar* mon_pc; + + FD_ZERO(&qcop_socket); + FD_SET(data->qconn->socket,&qcop_socket); + + while(1) + { + /* qconn is ours for the time being */ + pthread_mutex_lock(&data->qconn->access_mutex); + + /* wait for one second with each select() */ + tv.tv_sec = 1; + tv.tv_usec = 0; + + retval=select(data->qconn->socket+1,&qcop_socket,NULL,NULL,&tv); + if (retval > 0) + { + mon_pc = get_line(data->qconn); + if ( mon_pc ) + { + if (strstr(mon_pc,"cancelSync()")) + { + g_free(mon_pc); + data->qconn->syncing = FALSE; + (data->cancel_routine)(); + } + else + { + perror("Error on select() call or no data"); + g_free(mon_pc); + } + } + } + + /* we are done */ + if (!data->qconn->syncing) + { + pthread_mutex_unlock(&data->qconn->access_mutex); + g_free(data); + pthread_exit(NULL); + } + + /* unlock for a moment allow access for other threads */ + pthread_mutex_unlock(&data->qconn->access_mutex); + sleep(1); + } } Modified: plugins/opie-sync/src/opie_qcop.h ============================================================================== --- plugins/opie-sync/src/opie_qcop.h Fri Sep 25 21:44:39 2009 (r5852) +++ plugins/opie-sync/src/opie_qcop.h Sat Sep 26 15:37:36 2009 (r5853) @@ -32,24 +32,26 @@ typedef struct { - gboolean result; /* result of the previous operation */ - char* resultmsg; /* string associated with the result (error msg, etc) */ - int socket; /* socket open to the QCopBridge server */ - gboolean syncing; /* Tells us whether we are currently syncing */ - pthread_mutex_t access_mutex; /* At times we have two threads sharing access, so we need this */ + gboolean result; /* result of the previous operation */ + char* resultmsg; /* string associated with the result (error msg, etc) */ + int socket; /* socket open to the QCopBridge server */ + gboolean syncing; /* Tells us whether we are currently syncing */ + pthread_mutex_t access_mutex; /* At times we have two threads sharing access, so we need this */ } qcop_conn; typedef struct { - qcop_conn* qconn; /* the socket */ - void (*cancel_routine)(); /* routine to call if the user cancels the sync */ + qcop_conn* qconn; /* the socket */ + void (*cancel_routine)(); /* routine to call if the user cancels the sync */ } qcop_monitor_data; qcop_conn* qcop_connect(gchar* addr, gchar* username, gchar* password); void qcop_disconnect(qcop_conn* qconn); void qcop_freeqconn(qcop_conn* qconn); +gboolean qcop_send(qcop_conn* qconn, const char *message, const char *args); +char *qcop_wait_for(qcop_conn* qconn, const char *message); char* qcop_get_root(qcop_conn* qconn); void qcop_start_sync(qcop_conn* qconn, void (*cancel_routine)()); void qcop_stop_sync(qcop_conn* qconn); |