File: logic.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (220 lines) | stat: -rw-r--r-- 6,441 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 Copyright (©) 2003-2025 Teus Benschop.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <sendreceive/logic.h>
#include <filter/url.h>
#include <filter/date.h>
#include <filter/string.h>
#include <tasks/logic.h>
#include <database/config/general.h>
#include <database/config/bible.h>
#include <database/logs.h>
#include <database/bibles.h>
#include <client/logic.h>
#include <config/globals.h>


void sendreceive_queue_bible (std::string bible)
{
  tasks_logic_queue (task::send_receive_bibles, {bible});
}


// If the $minute is negative, it syncs right now.
// If the $minute is zero or higher, it determines from the settings whether and what to sync.
void sendreceive_queue_sync (int minute, int second)
{
  // The flags for which data to synchronize now.
  bool sync_bibles = false;
  bool sync_rest = false;
  
  // Deal with a numerical minute to find out whether it's time to automatically sync.
  if (minute >= 0) {

    int repeat = database::config::general::get_repeat_send_receive ();

    // Sync everything every hour.
    if ((repeat == 1) && (second == 0)) {
      minute = minute % 60;
      if (minute == 0) {
        sync_bibles = true;
        sync_rest = true;
      }
    }

    // Sync everything every five minutes.
    if ((repeat == 2) || (repeat == 3)) {
      if (second == 0) {
        minute = minute % 5;
        if (minute == 0) {
          sync_bibles = true;
          sync_rest = true;
        }
      }
    }

  }

  
  // Send and receive manually.
  if (minute < 0) {
    sync_bibles = true;
    sync_rest = true;
  }
  
  
  // Send / receive only works in Client mode.
  if (client_logic_client_enabled ()) {

    // Only queue a sync task if it is not running at the moment.
    if (sync_bibles) {
      if (tasks_logic_queued (task::sync_bibles)) {
        Database_Logs::log ("About to start synchronizing Bibles");
      } else {
        tasks_logic_queue (task::sync_bibles);
      }
    }
    if (sync_rest) {
      if (tasks_logic_queued (task::sync_notes)) {
        Database_Logs::log ("About to start synchronizing Notes");
      } else {
        tasks_logic_queue (task::sync_notes);
      }
      if (tasks_logic_queued (task::sync_settings)) {
        Database_Logs::log ("About to start synchronizing Settings");
      } else {
        tasks_logic_queue (task::sync_settings);
      }
      if (tasks_logic_queued (task::sync_changes)) {
        Database_Logs::log ("About to start synchronizing Changes");
      } else {
        tasks_logic_queue (task::sync_changes);
      }
      if (tasks_logic_queued (task::sync_files)) {
        Database_Logs::log ("About to start synchronizing Files");
      } else {
        tasks_logic_queue (task::sync_files);
      }
      // Sync resources always, because it checks on its own whether to do something.
      tasks_logic_queue (task::sync_resources);
    }

    if (sync_bibles || sync_rest) {
      // Store the most recent time that the sync action ran.
      database::config::general::set_last_send_receive (filter::date::seconds_since_epoch ());
    }
  }
}


// Checks whether any of the sync tasks has been queued.
// Returns the result as a boolean.
bool sendreceive_sync_queued ()
{
  if (tasks_logic_queued (task::sync_notes)) return true;
  if (tasks_logic_queued (task::sync_bibles)) return true;
  if (tasks_logic_queued (task::sync_settings)) return true;
  if (tasks_logic_queued (task::sync_changes)) return true;
  if (tasks_logic_queued (task::sync_files)) return true;
  return false;
}


// Queues Paratext sync.
void sendreceive_queue_paratext (tasks::enums::paratext_sync method)
{
#ifdef HAVE_PARATEXT
  if (sendreceive_paratext_queued ()) {
    Database_Logs::log ("About to start synchronizing with Paratext");
  } else {
    tasks_logic_queue (task::sync_paratext, { std::to_string(static_cast<int>(method)) });
  }
#endif
  (void) method;
}


// Checks whether the Paratext sync task has been queued.
// Returns the result as a boolean.
bool sendreceive_paratext_queued ()
{
  if (tasks_logic_queued (task::sync_paratext)) return true;
  return false;
}


void sendreceive_queue_all (bool now)
{
  std::vector <std::string> bibles = database::bibles::get_bibles ();
  for (auto & bible : bibles) {
    if (database::config::bible::get_remote_repository_url (bible) != "") {
      if (database::config::bible::get_repeat_send_receive (bible) || now) {
        sendreceive_queue_bible (bible);
      }
    }
  }
}


// Function that looks if, at app startup, it needs to queue sync operations in the client.
void sendreceive_queue_startup ()
{
  // Next second when it is supposed to sync.
  int next_second = database::config::general::get_last_send_receive ();

  // Check how often to repeat the sync action.
  int repeat = database::config::general::get_repeat_send_receive ();
  if (repeat == 1) {
    // Repeat every hour.
    next_second += 3600;
  }
  else if (repeat == 2) {
    // Repeat every five minutes.
    next_second += 300;
  } else {
    // No repetition.
    return;
  }
  
  // When the current time is past the next time it is supposed to sync, start the sync.
  if (filter::date::seconds_since_epoch () >= next_second) {
    sendreceive_queue_sync (-1, 0);
  }
}


// Returns true if any prioritized task is now active.
bool sendreceive_logic_prioritized_task_is_active ()
{
  if (   config_globals_syncing_bibles
      || config_globals_syncing_notes
      || config_globals_syncing_settings
      || config_globals_syncing_changes) {
    return true;
  }
  return false;
}


// Returns true if Bibledit Cloud has been linked to an external git repository.
bool sendreceive_git_repository_linked (std::string bible)
{
  std::string url = database::config::bible::get_remote_repository_url (bible);
  return !url.empty ();
}