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 <cmake.h>
28 #include <Datetime.h>
29 #include <format.h>
30 #include <commands.h>
31 #include <timew.h>
32 #include <iostream>
33 #include <stdlib.h>
34 
35 ////////////////////////////////////////////////////////////////////////////////
CmdMove(const CLI & cli,Rules & rules,Database & database,Journal & journal)36 int CmdMove (
37   const CLI& cli,
38   Rules& rules,
39   Database& database,
40   Journal& journal)
41 {
42   const bool verbose = rules.getBoolean ("verbose");
43 
44   // Gather ID and TAGs.
45   std::set <int> ids = cli.getIds ();
46 
47   if (ids.size() > 1)
48   {
49     throw std::string("The 'move' command only supports a single ID.");
50   }
51 
52   if (ids.empty())
53   {
54     throw std::string ("ID must be specified. See 'timew help move'.");
55   }
56 
57   journal.startTransaction ();
58 
59   int id = *ids.begin ();
60 
61   std::string new_start;
62   for (auto& arg : cli._args)
63   {
64     if (arg.hasTag ("FILTER") && arg._lextype == Lexer::Type::date)
65     {
66       new_start = arg.attribute ("raw");
67     }
68   }
69 
70   std::vector <Interval> intervals = getIntervalsByIds (database, rules, ids);
71   Interval interval = intervals.at (0);
72 
73   if (interval.synthetic)
74   {
75     flattenDatabase (database, rules);
76     intervals = getIntervalsByIds (database, rules, ids);
77     interval = intervals.at (0);
78   }
79 
80   // Move start time.
81   Datetime start (new_start);
82 
83   // Changing the start date should also change the end date by the same
84   // amount.
85   if (interval.start < start)
86   {
87     auto delta = start - interval.start;
88     interval.start = start;
89     if (! interval.is_open ())
90     {
91       interval.end += delta;
92     }
93   }
94   else
95   {
96     auto delta = interval.start - start;
97     interval.start = start;
98     if (! interval.is_open ())
99     {
100       interval.end -= delta;
101     }
102   }
103 
104   database.deleteInterval (intervals.at (0));
105 
106   validate (cli, rules, database, interval);
107   database.addInterval (interval, verbose);
108 
109   journal.endTransaction ();
110 
111   if (verbose)
112   {
113     std::cout << "Moved @" << id << " to " << interval.start.toISOLocalExtended () << '\n';
114   }
115 
116   return 0;
117 }
118 
119 ////////////////////////////////////////////////////////////////////////////////
120