diff --git a/src/Context.cpp b/src/Context.cpp index c3da451da..56b7ddca3 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -276,7 +276,7 @@ int Context::run () << " init:" << timer_init.total () << " load:" << timer_load.total () - << " synch:" << timer_synch.total () + << " synch:" << timer_sync.total () << " gc:" << timer_gc.total () << " filter:" << timer_filter.total () << " commit:" << timer_commit.total () @@ -284,7 +284,7 @@ int Context::run () << " render:" << timer_render.total () << " total:" << (timer_init.total () + timer_load.total () + - timer_synch.total () + + timer_sync.total () + timer_gc.total () + timer_filter.total () + timer_commit.total () + diff --git a/src/Context.h b/src/Context.h index 1aa447ad0..49cddca45 100644 --- a/src/Context.h +++ b/src/Context.h @@ -117,7 +117,7 @@ public: Timer timer_init; Timer timer_load; - Timer timer_synch; + Timer timer_sync; Timer timer_gc; Timer timer_filter; Timer timer_commit; diff --git a/src/commands/CmdSync.cpp b/src/commands/CmdSync.cpp index d31982794..90a8bcffb 100644 --- a/src/commands/CmdSync.cpp +++ b/src/commands/CmdSync.cpp @@ -25,13 +25,13 @@ // //////////////////////////////////////////////////////////////////////////////// -// TODO Localize new messages, when they stabilize. #define L10N // Localization complete. #include #include #include #include +#include #include #include #include @@ -52,6 +52,7 @@ CmdSync::CmdSync () int CmdSync::execute (std::string& output) { int status = 0; + context.timer_sync.start (); // If no server is set up, quit. std::string connection = context.config.get ("taskd.server"); @@ -75,36 +76,49 @@ int CmdSync::execute (std::string& output) if (backlog.exists ()) backlog.read (payload); + // Count the number of tasks being uploaded. + int upload_count = 0; + { + std::vector lines; + split (lines, payload, "\n"); + std::vector ::iterator i; + for (i = lines.begin (); i != lines.end (); ++i) + if ((*i)[0] == '[') + ++upload_count; + } + // Send 'sync' + payload. - Msg request, response; + Msg request; request.set ("protocol", "v1"); request.set ("type", "sync"); request.set ("org", credentials[0]); request.set ("user", credentials[1]); request.set ("key", credentials[2]); - // TODO Add the other header fields. + // TODO Add the other necessary header fields. request.setPayload (payload); - std::cout << "# request:\n" - << request.serialize (); - context.debug ("sync with " + connection); + std::cout << format (STRING_CMD_SYNC_PROGRESS, connection) + << "\n"; + + Msg response; if (send (connection, request, response)) { - std::cout << "# response:\n" - << response.serialize (); std::string code = response.get ("code"); if (code == "200") { + Color colorAdded (context.config.get ("color.sync.added")); + Color colorChanged (context.config.get ("color.sync.changed")); + + int download_count = 0; payload = response.getPayload (); std::vector lines; split (lines, payload, '\n'); - // Load all tasks. // TODO This is not necessary if only a synch key was received. - // TODO Furthermore, 'all' is not used. - std::vector all = context.tdb2.all_tasks (); + // Load all tasks. + context.tdb2.all_tasks (); std::string synch_key = ""; std::vector ::iterator line; @@ -112,6 +126,8 @@ int CmdSync::execute (std::string& output) { if ((*line)[0] == '[') { + ++download_count; + Task from_server (*line); std::string uuid = from_server.get ("uuid"); @@ -119,19 +135,21 @@ int CmdSync::execute (std::string& output) Task dummy; if (context.tdb2.get (uuid, dummy)) { - std::cout << " modify " - << uuid - << " '" - << from_server.get ("description") - << "'\n"; + std::cout << " " + << colorChanged.colorize ( + format (STRING_CMD_SYNC_MOD, + uuid, + from_server.get ("description"))) + << "\n"; context.tdb2.modify (from_server, false); } else { - std::cout << " add " - << uuid - << " '" - << from_server.get ("description") + std::cout << " " + << colorAdded.colorize ( + format (STRING_CMD_SYNC_ADD, + uuid, + from_server.get ("description"))) << "'\n"; context.tdb2.add (from_server, false); } @@ -158,28 +176,31 @@ int CmdSync::execute (std::string& output) // Commit all changes. context.tdb2.commit (); - context.footnote ("Sync successful."); - // TODO Sync successful. 2 tasks uploaded. - // TODO Sync successful. 4 tasks downloaded. - // TODO Sync successful. 2 tasks uploaded, 4 tasks downloaded. + // Present a clear status message. + if (upload_count == 0 && download_count == 0) + context.footnote (STRING_CMD_SYNC_SUCCESS0); + else if (upload_count == 0 && download_count > 0) + context.footnote (format (STRING_CMD_SYNC_SUCCESS2, download_count)); + else if (upload_count > 0 && download_count == 0) + context.footnote (format (STRING_CMD_SYNC_SUCCESS1, upload_count)); + else if (upload_count > 0 && download_count > 0) + context.footnote (format (STRING_CMD_SYNC_SUCCESS3, upload_count, download_count)); } } else if (code == "201") { - context.footnote ("Sync successful. No updates required."); + context.footnote (STRING_CMD_SYNC_SUCCESS_NOP); } else if (code == "430") { - context.error ("Sync failed. Either your credentials are incorrect, or " - "your Task Server account is not enabled."); + context.error (STRING_CMD_SYNC_FAIL_ACCOUNT); status = 2; } else { - context.error ("Sync failed. The Task Server returned error: " + - code + - " " + - response.get ("status")); + context.error (format (STRING_CMD_SYNC_FAIL_ERROR, + code, + response.get ("status"))); status = 2; } @@ -200,12 +221,14 @@ int CmdSync::execute (std::string& output) // - Wrong port // - Firewall // - Network error + // - No signal/cable else { - context.error ("Sync failed. Could not connect to the Task Server."); + context.error (STRING_CMD_SYNC_FAIL_CONNECT); status = 1; } + context.timer_sync.stop (); return status; } @@ -217,7 +240,7 @@ bool CmdSync::send ( { std::string::size_type colon = to.find (':'); if (colon == std::string::npos) - throw std::string ("ERROR: Malformed configuration setting '") + to + "'"; + throw format (STRING_CMD_SYNC_BAD_SERVER, to); std::string server = to.substr (0, colon); int port = strtoimax (to.substr (colon + 1).c_str (), NULL, 10); diff --git a/src/en-US.h b/src/en-US.h index 785393522..dbf2218df 100644 --- a/src/en-US.h +++ b/src/en-US.h @@ -413,6 +413,18 @@ #define STRING_CMD_SYNC_USAGE "Synchronizes data with the Task Server" #define STRING_CMD_SYNC_NO_SERVER "Task Server is not configured." #define STRING_CMD_SYNC_BAD_CRED "Task Server credentials malformed." +#define STRING_CMD_SYNC_ADD " add {1} '{2}'" +#define STRING_CMD_SYNC_MOD "modify {1} '{2}'" +#define STRING_CMD_SYNC_PROGRESS "Syncing with {1}" +#define STRING_CMD_SYNC_SUCCESS0 "Sync successful." +#define STRING_CMD_SYNC_SUCCESS1 "Sync successful. {1} changes uploaded." +#define STRING_CMD_SYNC_SUCCESS2 "Sync successful. {1} changes downloaded." +#define STRING_CMD_SYNC_SUCCESS3 "Sync successful. {1} changes uploaded, {2} changes downloaded." +#define STRING_CMD_SYNC_SUCCESS_NOP "Sync successful. No changes." +#define STRING_CMD_SYNC_FAIL_ACCOUNT "Sync failed. Either your credentials are incorrect, or your Task Server account is not enabled." +#define STRING_CMD_SYNC_FAIL_ERROR "Sync failed. The Task Server returned error: {1} {2}" +#define STRING_CMD_SYNC_FAIL_CONNECT "Sync failed. Could not connect to the Task Server." +#define STRING_CMD_SYNC_BAD_SERVER "Sync failed. Malformed configuration setting '{1}'" #define STRING_CMD_DIAG_USAGE "Platform, build and environment details" #define STRING_CMD_DIAG_PLATFORM "Platform" #define STRING_CMD_DIAG_UNKNOWN ""