Changeset 2478

Show
Ignore:
Timestamp:
08/20/07 11:12:12 (1 year ago)
Author:
paule
Message:

Add support for syncing with Opie-Notes via new notestype configuration option

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/opie-sync/src/opie-sync

    r2476 r2478  
    77  <port>4242</port> 
    88  <conntype>ftp</conntype> 
     9  <notestype>basic</notestype> 
    910</config> 
  • plugins/opie-sync/src/opie_comms.c

    r2476 r2478  
    230230        return NULL; 
    231231} 
     232 
     233 
     234char *get_remote_notes_path(OpiePluginEnv *env) 
     235{ 
     236        osync_trace(TRACE_ENTRY, "%s(%p)", __func__, env); 
     237        char* root_path; 
     238         
     239        if ( env->use_qcop ) { 
     240                root_path = qcop_get_root(env->qcopconn); 
     241                if(!root_path) { 
     242                        fprintf(stderr, "qcop_get_root: %s\n", env->qcopconn->resultmsg); 
     243                        osync_trace(TRACE_EXIT_ERROR, "qcop_get_root: %s", env->qcopconn->resultmsg); 
     244                        return NULL; 
     245                } 
     246                osync_trace( TRACE_INTERNAL, "QCop root path = %s", root_path ); 
     247        } 
     248        else 
     249                root_path = g_strdup( "" ); 
     250         
     251        char *notes_path; 
     252        if(env->notes_type == NOTES_TYPE_OPIE_NOTES) 
     253                notes_path = g_build_filename(root_path, "Documents/text/plain", NULL); 
     254        else 
     255                notes_path = g_strdup(root_path); 
     256         
     257        g_free(root_path); 
     258         
     259        osync_trace(TRACE_EXIT, "%s(%s)", __func__, notes_path); 
     260        return notes_path; 
     261} 
     262 
    232263 
    233264 
     
    647678        if (env->host && env->username && env->password ) 
    648679        { 
    649                 char* separator_path; 
    650                 if ( env->use_qcop )  
    651                 { 
    652                         char* root_path = qcop_get_root(env->qcopconn); 
    653                         if(!root_path) { 
    654                                 fprintf(stderr, "qcop_get_root: %s\n", env->qcopconn->resultmsg); 
    655                                 osync_trace(TRACE_EXIT_ERROR, "qcop_get_root: %s", env->qcopconn->resultmsg); 
    656                                 return FALSE; 
    657                         } 
    658                         osync_trace( TRACE_INTERNAL, "QCop root path = %s", root_path ); 
    659                         separator_path = g_strdup_printf("%s/", root_path); 
    660                         g_free(root_path); 
    661                 } else { 
    662                         separator_path = g_strdup( "/" ); 
    663                 } 
    664  
    665                 ftpurl = g_strdup_printf("ftp://%s:%s@%s:%u%s", 
     680                char *remotepath = get_remote_notes_path(env); 
     681                if(!remotepath) { 
     682                        osync_trace(TRACE_EXIT_ERROR, "%s: failed to get remote notes path", __func__); 
     683                        return FALSE; 
     684                } 
     685 
     686                ftpurl = g_strdup_printf("ftp://%s:%s@%s:%u%s/", 
    666687                                                 env->username, 
    667688                                                 env->password, 
    668689                                                 env->host, 
    669690                                                 env->device_port, 
    670                                                  separator_path); 
     691                                                 remotepath); 
    671692                         
    672693                /* curl init */ 
     
    677698                curl_easy_setopt(curl, CURLOPT_WRITEDATA, bufstr); 
    678699                curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, opie_curl_strwrite); 
     700                 
     701                osync_trace(TRACE_INTERNAL, "retrieving directory: %s", ftpurl); 
    679702                 
    680703                /* get the dir listing */ 
     
    694717                                                        GString *bufstr = g_string_new(""); 
    695718                                                        char *ftpfileurl = g_strdup_printf("%s/%s", ftpurl, ptr); 
     719                                                        osync_trace(TRACE_INTERNAL, "retrieving file: %s", ftpfileurl); 
    696720                                                        curl_easy_setopt(curl, CURLOPT_URL, ftpfileurl); 
    697721                                                        curl_easy_setopt(curl, CURLOPT_WRITEDATA, bufstr); 
    698722                                                        res = curl_easy_perform(curl); 
     723                                                        osync_trace(TRACE_INTERNAL, "done retrieving, result = %i", res); 
    699724                                                        g_free(ftpfileurl); 
    700725                                                        /* Remove .txt from end of file name */ 
     
    731756                g_free(ftpurl); 
    732757                curl_easy_cleanup(curl); 
    733                 g_free(separator_path); 
     758                g_free(remotepath); 
    734759        } 
    735760        else 
     
    750775        CURL *curl; 
    751776        CURLcode res; 
    752         char* separator_path; 
    753777        char *ftpurl; 
    754778         
    755779        if (env->host && env->username && env->password ) 
    756780        { 
    757                 if ( env->use_qcop )  
    758                 { 
    759                         char* root_path = qcop_get_root(env->qcopconn); 
    760                         if(!root_path) { 
    761                                 fprintf(stderr, "qcop_get_root: %s\n", env->qcopconn->resultmsg); 
    762                                 osync_trace(TRACE_EXIT_ERROR, "qcop_get_root: %s", env->qcopconn->resultmsg); 
    763                                 return FALSE; 
    764                         } 
    765                         osync_trace( TRACE_INTERNAL, "QCop root path = %s", root_path ); 
    766                         separator_path = g_strdup_printf("%s/", root_path); 
    767                         g_free(root_path); 
    768                 } else { 
    769                         separator_path = g_strdup( "/" ); 
    770                 } 
    771                  
     781                char *remotepath = get_remote_notes_path(env); 
     782                if(!remotepath) { 
     783                        osync_trace(TRACE_EXIT_ERROR, "%s: failed to get remote notes path", __func__); 
     784                        return FALSE; 
     785                } 
     786 
    772787                xmlNode *node = opie_xml_get_first(doc, "notes", "note"); 
    773788                while(node) { 
     
    790805                                                                        env->host, 
    791806                                                                        env->device_port, 
    792                                                                         separator_path); 
     807                                                                        remotepath); 
    793808                                                 
    794809                                                struct curl_slist *cmdlist = NULL; 
    795                                                 char *command = g_strdup_printf("DELE %s%s.txt", separator_path, notename); 
     810                                                char *command = g_strdup_printf("DELE %s/%s.txt", remotepath, notename); 
    796811                                                cmdlist = curl_slist_append(cmdlist, command); 
    797812                                                curl_easy_setopt(curl, CURLOPT_QUOTE, cmdlist); 
     
    800815                                        else { 
    801816                                                /* Changed note, upload it */ 
    802                                                 ftpurl = g_strdup_printf("ftp://%s:%s@%s:%u%s%s.txt", 
     817                                                ftpurl = g_strdup_printf("ftp://%s:%s@%s:%u%s/%s.txt", 
    803818                                                                        env->username, 
    804819                                                                        env->password, 
    805820                                                                        env->host, 
    806821                                                                        env->device_port, 
    807                                                                         separator_path, 
     822                                                                        remotepath, 
    808823                                                                        notename); 
    809824                                                 
     
    845860                } 
    846861                 
    847                 g_free(separator_path); 
     862                g_free(remotepath); 
    848863        } 
    849864        else 
     
    10021017                curl_easy_setopt(curl, CURLOPT_READDATA, data); 
    10031018                curl_easy_setopt(curl, CURLOPT_READFUNCTION, opie_curl_strread); 
    1004                  
     1019                m_totalwritten = 0; 
    10051020                 
    10061021                curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1); 
     
    12331248        char* scpcommand = NULL; 
    12341249        int scpretval, scpexitstatus; 
     1250        char *remotepath = NULL; 
    12351251         
    12361252        if(env->host && env->device_port && env->username) { 
    1237                 char* separator_path; 
    1238                 if ( env->use_qcop )  
    1239                 { 
    1240                         char* root_path = qcop_get_root(env->qcopconn); 
    1241                         if(!root_path) { 
    1242                                 fprintf(stderr, "qcop_get_root: %s\n", env->qcopconn->resultmsg); 
    1243                                 osync_trace(TRACE_EXIT_ERROR, "qcop_get_root: %s", env->qcopconn->resultmsg); 
    1244                                 return FALSE; 
    1245                         } 
    1246                         osync_trace( TRACE_INTERNAL, "QCop root path = %s", root_path ); 
    1247                         separator_path = g_strdup_printf("%s/", root_path); 
    1248                         g_free(root_path); 
    1249                 } else { 
    1250                         separator_path = g_strdup( "/" ); 
     1253                remotepath = get_remote_notes_path(env); 
     1254                if(!remotepath) { 
     1255                        osync_trace(TRACE_EXIT_ERROR, "%s: failed to get remote notes path", __func__); 
     1256                        return FALSE; 
    12511257                } 
    12521258                 
    12531259                /* Create a temp directory */ 
    1254                 char *randstr = g_strdup_printf("opie-sync-%i", g_random_int_range(0, G_MAXUINT32)); 
     1260                char *randstr = g_strdup_printf("opie-sync-%i", g_random_int_range(0, 2000000000)); 
    12551261                char *temppath = g_build_filename(g_get_tmp_dir(), randstr); 
    12561262                g_free(randstr); 
     
    12661272                  this with scp unfortunately because scp seems to return 1  
    12671273                  on any error */ 
    1268                 scpcommand = g_strdup_printf("ssh -o BatchMode=yes %s@%s \"ls %s*.txt > /dev/null\"", 
     1274                scpcommand = g_strdup_printf("ssh -o BatchMode=yes %s@%s \"ls %s/*.txt > /dev/null\"", 
    12691275                                                                                                                                env->username, 
    12701276                                                                                                                                env->host, 
    1271                                                                                                                                 separator_path); 
     1277                                                                                                                                remotepath); 
    12721278                 
    12731279                scpretval = pclose(popen(scpcommand,"w")); 
     
    12831289                         
    12841290                        /* Fetch all text files from the remote path into the temp directory */ 
    1285                         scpcommand = g_strdup_printf("scp -p -q -B %s@%s:%s*.txt %s", 
     1291                        scpcommand = g_strdup_printf("scp -p -q -B %s@%s:%s/*.txt %s", 
    12861292                                                                                                                                        env->username, 
    12871293                                                                                                                                        env->host, 
    1288                                                                                                                                         separator_path, 
     1294                                                                                                                                        remotepath, 
    12891295                                                                                                                                        temppath); 
    12901296                         
     
    13151321        if(scpcommand); 
    13161322                g_free(scpcommand); 
     1323                 
     1324        if(remotepath) 
     1325                g_free(remotepath); 
    13171326         
    13181327        osync_trace(TRACE_EXIT, "%s(%i)", __func__, rc ); 
     
    13311340        int scpretval, scpexitstatus; 
    13321341        char *temppath = NULL; 
    1333         char *separator_path = NULL; 
     1342        char *remotepath = NULL; 
    13341343         
    13351344        if(env->host && env->device_port && env->username) { 
    1336                 if ( env->use_qcop )  
    1337                 { 
    1338                         char* root_path = qcop_get_root(env->qcopconn); 
    1339                         if(!root_path) { 
    1340                                 fprintf(stderr, "qcop_get_root: %s\n", env->qcopconn->resultmsg); 
    1341                                 osync_trace(TRACE_EXIT_ERROR, "qcop_get_root: %s", env->qcopconn->resultmsg); 
    1342                                 return FALSE; 
    1343                         } 
    1344                         osync_trace( TRACE_INTERNAL, "QCop root path = %s", root_path ); 
    1345                         separator_path = g_strdup_printf("%s/", root_path); 
    1346                         g_free(root_path); 
    1347                 } else { 
    1348                         separator_path = g_strdup( "/" ); 
     1345                remotepath = get_remote_notes_path(env); 
     1346                if(!remotepath) { 
     1347                        osync_trace(TRACE_EXIT_ERROR, "%s: failed to get remote notes path", __func__); 
     1348                        return FALSE; 
    13491349                } 
    13501350                 
     
    13681368                 
    13691369                /* create remote path */ 
    1370                 scpcommand = g_strdup_printf("ssh -o BatchMode=yes %s@%s \"mkdir -p %s\"", 
     1370                scpcommand = g_strdup_printf("ssh -o BatchMode=yes %s@%s \"mkdir -p %s/\"", 
    13711371                                                                                                                                env->username, 
    13721372                                                                                                                                env->host, 
    1373                                                                                                                                 separator_path); 
     1373                                                                                                                                remotepath); 
    13741374                 
    13751375                scpretval = pclose(popen(scpcommand,"w")); 
     
    13881388                                                                                                                                env->username, 
    13891389                                                                                                                                env->host, 
    1390                                                                                                                                 separator_path); 
     1390                                                                                                                                remotepath); 
    13911391                 
    13921392                scpretval = pclose(popen(scpcommand,"w")); 
     
    14231423                if(deletedfiles->len > 0) { 
    14241424                        g_free(scpcommand); 
    1425                         scpcommand = g_strdup_printf("ssh -o BatchMode=yes %s@%s \"cd %s && rm -f %s\"", 
     1425                        scpcommand = g_strdup_printf("ssh -o BatchMode=yes %s@%s \"cd %s/ && rm -f %s\"", 
    14261426                                                                                                                                        env->username, 
    14271427                                                                                                                                        env->host, 
    1428                                                                                                                                         separator_path, 
     1428                                                                                                                                        remotepath, 
    14291429                                                                                                                                        deletedfiles->str); 
    14301430                         
     
    14511451        } 
    14521452         
    1453         if(separator_path) 
    1454                 g_free(separator_path); 
     1453        if(remotepath) 
     1454                g_free(remotepath); 
    14551455         
    14561456        if(scpcommand); 
  • plugins/opie-sync/src/opie_sync.c

    r2476 r2478  
    6161        env->backupdir = NULL; 
    6262        env->localdir = g_strdup("/tmp"); 
     63        env->notes_type = NOTES_TYPE_BASIC; 
    6364 
    6465        doc = xmlParseMemory(config, strlen(config)); 
     
    125126                                        g_free(env->localdir); 
    126127                                        env->localdir = g_strdup(str); 
     128                                } else if (!xmlStrcmp(cur->name, (const xmlChar *)"notestype")) { 
     129                                        if ( (!strcasecmp(str, "opie-notes")) || (!strcasecmp(str, "opie_notes")) ) 
     130                                                env->notes_type = NOTES_TYPE_OPIE_NOTES; 
     131                                        else if ( strcasecmp(str, "basic") == 0 ) 
     132                                                env->notes_type = NOTES_TYPE_BASIC; 
     133                                        else { 
     134                                                osync_error_set(error, OSYNC_ERROR_GENERIC, "Invalid value \"%s\" for configuration option \"%s\"", str, cur->name); 
     135                                                goto error_free_doc; 
     136                                        } 
    127137                                } else { 
    128138                                        osync_error_set(error, OSYNC_ERROR_GENERIC, "Invalid configuration file option \"%s\"", cur->name); 
     
    275285                goto error; 
    276286 
     287        if(env->objtype == OPIE_OBJECT_TYPE_NOTE) { 
     288                /* Check if the notestype config option has changed since the last sync */ 
     289                char *anchorpath = g_strdup_printf("%s/anchor.db", osync_plugin_info_get_configdir(info)); 
     290                char *notes_type_str = g_strdup_printf("%d", env->plugin_env->notes_type); 
     291                if (!osync_anchor_compare(anchorpath, "notestype", notes_type_str)) 
     292                        osync_objtype_sink_set_slowsync(sink, TRUE); 
     293                g_free(notes_type_str); 
     294                g_free(anchorpath); 
     295        } 
     296         
    277297        osync_context_report_success(ctx); 
    278298        osync_trace(TRACE_EXIT, "%s", __func__); 
     
    532552        } 
    533553         
     554        if(env->objtype == OPIE_OBJECT_TYPE_NOTE) { 
     555                char *anchorpath = g_strdup_printf("%s/anchor.db", osync_plugin_info_get_configdir(info)); 
     556                char *notes_type_str = g_strdup_printf("%d", env->plugin_env->notes_type); 
     557                osync_anchor_update(anchorpath, "notestype", notes_type_str); 
     558                g_free(notes_type_str); 
     559                g_free(anchorpath); 
     560        } 
     561         
    534562        osync_context_report_success(ctx); 
    535563        osync_trace(TRACE_EXIT, "%s", __func__); 
  • plugins/opie-sync/src/opie_sync.h

    r2476 r2478  
    5050 
    5151typedef enum { 
     52    NOTES_TYPE_OPIE_NOTES, 
     53    NOTES_TYPE_BASIC 
     54} OPIE_NOTES_TYPE; 
     55 
     56typedef enum { 
    5257  OPIE_OBJECT_TYPE_UNKNOWN, 
    5358  OPIE_OBJECT_TYPE_CONTACT, 
     
    6974        gchar*            backuppath;   /* the full path to the backup dir for this session */ 
    7075        gchar*            localdir;     /* path to local files if conntype = OPIE_CONN_NONE (for debugging) */ 
     76        OPIE_NOTES_TYPE   notes_type; 
    7177 
    7278        gboolean          use_qcop;