From f470acadaa3d3ac13706d0268b52718cc2e776ac Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 14 Jun 2009 14:26:39 -0400 Subject: [PATCH] Enhancements - export - Implemented Task::composeCSV. - Implemented export command, but removed filename support. This needs to be documented. --- src/Context.cpp | 4 ++- src/Task.cpp | 32 ++++++++++++++++++-- src/command.cpp | 80 ++++++++++++++++++++----------------------------- src/report.cpp | 4 ++- 4 files changed, 69 insertions(+), 51 deletions(-) diff --git a/src/Context.cpp b/src/Context.cpp index 1c59c64c6..40e77fc75 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -164,7 +164,6 @@ std::string Context::dispatch () // TODO Look at this thing. It just cries out for a dispatch table. /* - if (cmd.command == "export") { out = handleExport (); } */ if (cmd.command == "projects") { out = handleProjects (); } else if (cmd.command == "tags") { out = handleTags (); } @@ -191,6 +190,9 @@ std::string Context::dispatch () else if (cmd.command == "start") { out = handleStart (); } else if (cmd.command == "stop") { out = handleStop (); } else if (cmd.command == "undo") { out = handleUndo (); } +*/ + else if (cmd.command == "export") { out = handleExport (); } +/* else if (cmd.command == "import") { out = handleImport (); } else if (cmd.command == "duplicate") { out = handleDuplicate (); } else if (cmd.command == "edit") { out = handleEdit (); } diff --git a/src/Task.cpp b/src/Task.cpp index 51b75f7fe..e3dcad082 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -298,8 +298,36 @@ void Task::legacyParse (const std::string& line) //////////////////////////////////////////////////////////////////////////////// std::string Task::composeCSV () { - throw std::string ("unimplemented Task::composeCSV"); - return ""; + std::stringstream out; + + out << "'" << id << "',"; + out << "'" << get ("uuid") << "',"; + out << "'" << get ("status") << "',"; + + // Tags + std::vector tags; + getTags (tags); + std::string allTags; + join (allTags, " ", tags); + out << "'" << allTags << "',"; + + out << "'" << get ("entry") << "',"; + out << "'" << get ("start") << "',"; + out << "'" << get ("due") << "',"; + out << "'" << get ("recur") << "',"; + out << "'" << get ("end") << "',"; + out << "'" << get ("project") << "',"; + out << "'" << get ("priority") << "',"; + out << "'" << get ("fg") << "',"; + out << "'" << get ("bg") << "',"; + + // Convert single quotes to double quotes, because single quotes are used to + // delimit the values that need it. + std::string clean = get ("description"); + std::replace (clean.begin (), clean.end (), '\'', '"'); + out << "'" << clean << "'\n"; + + return out.str (); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/command.cpp b/src/command.cpp index 1e79461fd..4df8927b3 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -639,58 +639,44 @@ std::string handleDone () //////////////////////////////////////////////////////////////////////////////// std::string handleExport () { - std::stringstream output; -/* - // Use the description as a file name, then clobber the description so the - // file name isn't used for filtering. - std::string file = trim (task.getDescription ()); - task.setDescription (""); + std::stringstream out; - if (file.length () > 0) + out << "'id'," + << "'uuid'," + << "'status'," + << "'tags'," + << "'entry'," + << "'start'," + << "'due'," + << "'recur'," + << "'end'," + << "'project'," + << "'priority'," + << "'fg'," + << "'bg'," + << "'description'" + << "\n"; + + int count = 0; + + // Get all the tasks. + std::vector tasks; + context.tdb.lock (context.config.get ("locking", true)); + context.tdb.loadPending (tasks, context.filter); + context.tdb.unlock (); + // TODO handleRecurrence (tdb, tasks); + + foreach (task, tasks) { - std::ofstream out (file.c_str ()); - if (out.good ()) + if (task->getStatus () != Task::recurring && + task->getStatus () != Task::deleted) { - out << "'id'," - << "'uuid'," - << "'status'," - << "'tags'," - << "'entry'," - << "'start'," - << "'due'," - << "'recur'," - << "'end'," - << "'project'," - << "'priority'," - << "'fg'," - << "'bg'," - << "'description'" - << "\n"; - - int count = 0; - std::vector all; - tdb.allPendingT (all); - filter (all, task); - foreach (t, all) - { - if (t->getStatus () != T::recurring && - t->getStatus () != T::deleted) - { - out << t->composeCSV ().c_str (); - ++count; - } - } - out.close (); - - output << count << " tasks exported to '" << file << "'" << std::endl; + out << task->composeCSV ().c_str (); + ++count; } - else - throw std::string ("Could not write to export file."); } - else - throw std::string ("You must specify a file to write to."); -*/ - return output.str (); + + return out.str (); } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/report.cpp b/src/report.cpp index 92fb55bfe..6492a6325 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -174,7 +174,7 @@ std::string shortUsage () row = table.addRow (); table.addCell (row, 1, "task export"); - table.addCell (row, 2, "Exports all tasks as a CSV file"); + table.addCell (row, 2, "Lists all tasks as a CSV file"); row = table.addRow (); table.addCell (row, 1, "task color"); @@ -274,6 +274,7 @@ std::string handleInfo () context.tdb.lock (context.config.get ("locking", true)); context.tdb.loadPending (tasks, context.filter); context.tdb.unlock (); + // TODO handleRecurrence (tdb, tasks); // Filter sequence. context.filter.applySequence (tasks, context.sequence); @@ -1615,6 +1616,7 @@ std::string handleReportStats () context.tdb.lock (context.config.get ("locking", true)); context.tdb.load (tasks, context.filter); context.tdb.unlock (); + // TODO handleRecurrence (tdb, tasks); Date now; time_t earliest = time (NULL);