diff --git a/src/Task.cpp b/src/Task.cpp index 628db4176..b5c1a72c9 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -151,6 +151,19 @@ Task::Task (const std::string& input) parse (input); } +//////////////////////////////////////////////////////////////////////////////// +Task::Task (const json::object* obj) +{ + id = 0; + urgency_value = 0.0; + recalc_urgency = true; + is_blocked = false; + is_blocking = false; + annotation_count = 0; + + parseJSON (obj); +} + //////////////////////////////////////////////////////////////////////////////// Task::~Task () { diff --git a/src/Task.h b/src/Task.h index edb04a8a8..081ad4919 100644 --- a/src/Task.h +++ b/src/Task.h @@ -63,6 +63,7 @@ public: Task& operator= (const Task&); // Assignment operator bool operator== (const Task&); // Comparison operator Task (const std::string&); // Parse + Task (const json::object*); // Parse ~Task (); // Destructor void parse (const std::string&); diff --git a/src/commands/CmdImport.cpp b/src/commands/CmdImport.cpp index b707c1003..e75d84824 100644 --- a/src/commands/CmdImport.cpp +++ b/src/commands/CmdImport.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -64,17 +63,15 @@ int CmdImport::execute (std::string& output) std::vector words = context.cli2.getWords (); if (! words.size () || (words.size () == 1 && words[0] == "-")) { - // No files or only "-" specified, import tasks from STDIN. - std::vector lines; - std::string line; - std::cout << format (STRING_CMD_IMPORT_FILE, "STDIN") << "\n"; + std::string json; + std::string line; while (std::getline (std::cin, line)) - lines.push_back (line); + json += line + " "; - if (lines.size () > 0) - count = import (lines); + if (nontrivial (json)) + count = import (json); } else { @@ -88,10 +85,10 @@ int CmdImport::execute (std::string& output) std::cout << format (STRING_CMD_IMPORT_FILE, word) << "\n"; // Load the file. - std::vector lines; - incoming.read (lines); - - count += import (lines); + std::string json; + incoming.read (json); + if (nontrivial (json)) + count += import (json); } } @@ -158,3 +155,84 @@ int CmdImport::import (std::vector & lines) } //////////////////////////////////////////////////////////////////////////////// +int CmdImport::import (const std::string& input) +{ + int count = 0; + json::value* root = json::parse (input); + + // Single object parse. Input looks like: + // { ... } + if (root->type () == json::j_object) + { + // For each object element... + json::object* root_obj = (json::object*)root; + if (root_obj) + { + importSingleTask (root_obj); + ++count; + } + } + + // Multiple object array. Input looks like: + // [ { ... } , { ... } ] + else if (root->type () == json::j_array) + { + json::array* root_arr = (json::array*)root; + + // For each object element... + for (auto& element : root_arr->_data) + { + // For each object element... + json::object* root_obj = (json::object*)element; + if (root_obj) + { + importSingleTask (root_obj); + ++count; + } + } + } + + delete root; + return count; +} + +//////////////////////////////////////////////////////////////////////////////// +void CmdImport::importSingleTask (json::object* obj) +{ + // Parse the whole thing. + Task task (obj); + + // Check whether the imported task is new or a modified existing task. + Task before; + if (context.tdb2.get (task.get ("uuid"), before)) + { + // "modified:" is automatically set to the current time when a task is + // changed. If the imported task has a modification timestamp we need + // to ignore it in taskDiff() in order to check for meaningful + // differences. Setting it to the previous value achieves just that. + task.set ("modified", before.get ("modified")); + if (taskDiff (before, task)) + { + CmdModify modHelper; + modHelper.checkConsistency (before, task); + modHelper.modifyAndUpdate (before, task); + std::cout << " mod "; + } + else + { + std::cout << " skip "; + } + } + else + { + context.tdb2.add (task); + std::cout << " add "; + } + + std::cout << task.get ("uuid") + << " " + << task.get ("description") + << "\n"; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdImport.h b/src/commands/CmdImport.h index 278fa0e2e..877340895 100644 --- a/src/commands/CmdImport.h +++ b/src/commands/CmdImport.h @@ -29,16 +29,8 @@ #include #include +#include -class CmdImport : public Command -{ -public: - CmdImport (); - int execute (std::string&); - int import (std::vector & lines); -}; - -/* class CmdImport : public Command { public: @@ -46,31 +38,10 @@ public: int execute (std::string&); private: - enum fileType - { - type_not_a_clue, - type_task_1_4_3, - type_task_1_5_0, - type_task_1_6_0, - type_task_cmd_line, - type_todo_sh_2_0, - type_csv, - type_yaml, - type_text - }; - - fileType determineFileType (const std::vector &); - void decorateTask (Task&); - std::string task_1_4_3 (const std::vector &); - std::string task_1_5_0 (const std::vector &); - std::string task_1_6_0 (const std::vector &); - std::string taskCmdLine (const std::vector &); - std::string todoSh_2_0 (const std::vector &); - std::string text (const std::vector &); - std::string CSV (const std::vector &); - std::string YAML (const std::vector &); + int import (std::vector &); + int import (const std::string&); + void importSingleTask (json::object*); }; -*/ #endif ////////////////////////////////////////////////////////////////////////////////