1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2016 - 2021, Thomas Lauf, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // https://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26 
27 #include <Duration.h>
28 #include <format.h>
29 #include <commands.h>
30 #include <timew.h>
31 #include <iostream>
32 
33 ////////////////////////////////////////////////////////////////////////////////
CmdFill(const CLI & cli,Rules & rules,Database & database,Journal & journal)34 int CmdFill (
35   const CLI& cli,
36   Rules& rules,
37   Database& database,
38   Journal& journal)
39 {
40   const bool verbose = rules.getBoolean ("verbose");
41 
42   std::set <int> ids = cli.getIds ();
43 
44   if (ids.empty ())
45   {
46     throw std::string ("IDs must be specified. See 'timew help fill'.");
47   }
48 
49   // Load the data.
50   // Note: There is no filter.
51   Interval filter;
52   auto tracked = getTracked (database, rules, filter);
53 
54   journal.startTransaction ();
55 
56   // Apply tags to ids.
57   for (auto& id : ids)
58   {
59     if (id > static_cast <int> (tracked.size ()))
60       throw format ("ID '@{1}' does not correspond to any tracking.", id);
61 
62     Interval from = tracked[tracked.size () - id];
63     std::cout << "# from " << from.dump () << "\n";
64     Interval to {from};
65 
66     database.deleteInterval (from);
67     autoFill (rules, database, to);
68     validate (cli, rules, database, to);
69     std::cout << "# to " << to.dump () << "\n";
70     database.addInterval (to, verbose);
71 
72     // Note: Feedback generated inside autoFill().
73   }
74 
75   journal.endTransaction ();
76 
77   return 0;
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81