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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
/*
* This file is part of Clinica.
*
* Clinica 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.
*
* Clinica 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 Clinica. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Leonardo Robol <leo@robol.it>
* Gianmarco Brocchi <brocchi@poisson.phc.unipi.it>
*/
using GLib;
using Sqlite;
using Gtk;
namespace Clinica {
/**
* @brief Type representing the callback for the error
* signal.
*/
public delegate void ErrorCallback(GLib.Object source, string message);
/**
* @brief Grant access to resources such as
* UI files, configuration and database
*/
public class ResourceManager : GLib.Object {
/**
* @brief The padding the should be used in windows
* in Clinica.
*/
public int PADDING = 6;
/**
* @brief GtkApplication of Clinica.
*/
internal Clinica.Application application;
/**
* @brief Callback that manages errors
* notification to the end user.
*/
public unowned ErrorCallback error_callback;
/**
* @brief PatientListStore that holds the list
* of the patients
*/
public PatientListStore patient_list_store;
/**
* @brief DoctorListStore that holds the list
* of the doctors.
*/
public DoctorListStore doctor_list_store;
/**
* @brief VisitListStore that holds the list
* of the visits.
*/
public VisitListStore visit_list_store;
/**
* @brief EventListStore that holds the list of
* the events scheduled in clinica.
*/
public EventListStore event_list_store;
/**
* @brief This is the object used to retrieve all the data
* about patients and which events should be connected
* to the views to make them react to changes in the database
* or whatever the data source is.
*/
internal DataProvider data_provider { get; private set; }
/**
* @brief A NetworkedDataProvider that can communicate with other clinica
* instances on the LAN.
*/
internal NetworkedDataProvider networked_data_provider;
/**
* @brief DataProvider bounded to the local database.
*/
internal SqliteDataProvider sqlite_data_provider;
/**
* @brief Reference to a user interface
*/
public UserInterface user_interface;
/**
* @brief Clinica settings
*/
public Settings settings;
/**
* @brief Signal emitted when an error
* occurr. The user should be notified
* with message by the application
*/
public signal void error (string message);
/**
* @brief Local configuration directory
* obtained at runtime.
*/
internal string local_config_directory;
/**
* @brief Local data directory obtained
* at runtime.
*/
internal string local_data_directory;
/**
* @brief The file store used to retrieve the data associated
* with the Visits.
*/
public FileStore file_store;
#if HAVE_PLUGINS
/**
* @brief PluginEngine for Clinica
*/
public PluginEngine plugin_engine;
#endif
/**
* @brief Array containing the valid medicine search engines that
* are available at the moments. This should be used by plugins to
* register themselves as valid search engines
*/
internal GLib.List<unowned MedicineSearchEngine> medicine_search_engines;
/**
* @brief List containing the available DataProviders that
* can be used to retrieve data in Clinica.
*/
internal List<DataProvider> data_providers;
/**
* @brief A pointer to the patient that is been dragged. Should
* be set on drag_begin by the treeview and reset to null by the
* drag destination handler.
*/
internal Patient? dragging_patient = null;
/**
* @brief A pointer to the DataServer instance if is activated, or null
* if is disabled.
*/
internal DataServer? data_server = null;
#if HAVE_PLUGINS
private Peas.ExtensionSet core_set;
#endif
internal bool persistency_required = false;
/**
* @brief Prefix where Clinica is installed.
*/
internal string prefix;
public const string doctor_table = "doctors";
public const string patient_table = "patients";
public const string visits_table = "visits";
public const string events_table = "events";
/** SIGNALS **/
internal signal void medicine_search_engines_added (MedicineSearchEngine engine);
internal signal void medicine_search_engines_removed (MedicineSearchEngine engine);
internal signal void data_provider_added (DataProvider provider);
internal signal void data_provider_removed (DataProvider provider);
public ResourceManager (Clinica.Application? application,
string program_name = "clinica",
ErrorCallback? callback = null){
if (callback != null)
error_callback = callback;
else {
error_callback = (source, message) => debug (message);
}
/* Save the GtkApplication */
this.application = application;
/* Connect error callback */
error.connect ((t,l) => callback(t,l));
/* Init databases and configuration files
* if needed. */
local_config_directory = Environment.get_user_config_dir ();
local_config_directory = Path.build_filename (local_config_directory,
"clinica");
/* Set local configuration directory */
local_data_directory = Environment.get_user_data_dir ();
local_data_directory = Path.build_filename (local_data_directory, "clinica");
/* Check if configuration directory exists */
if (!FileUtils.test (local_config_directory,
FileTest.IS_DIR)) {
DirUtils.create_with_parents (local_config_directory, -1);
}
if (!FileUtils.test (local_data_directory, FileTest.IS_DIR)) {
DirUtils.create_with_parents (local_data_directory, -1);
}
/* Check if the directory for the FileStore exists */
File fsd = File.new_for_path (get_filestore_dir ());
if (!fsd.query_exists ())
DirUtils.create_with_parents (get_filestore_dir (), -1);
file_store = new FileStore (this);
/* Find prefix */
prefix = "/usr/local";
if (!FileUtils.test (Path.build_filename(prefix, "bin", "clinica"), FileTest.IS_EXECUTABLE)) {
prefix = "/usr";
}
/* Try setting GSETTINGS_SCHEMA_DIR to this directory so if this is
* a local build or the Windows version we are able to use the in-place
* compiled schema.
*
* Do this only if the file "gschemas.compiled" is found.
*/
if (FileUtils.test ("gschemas.compiled", FileTest.EXISTS)) {
debug ("Settings GSETTINGS_SCHEMA_DIR to '.' since gschemas.compiled is found");
Environment.set_variable ("GSETTINGS_SCHEMA_DIR", ".", true);
}
/* Create configuration */
settings = new Settings (this);
/* SqliteDataProvider must always be the first DataProvider
* in the list */
data_providers = new List<DataProvider> ();
sqlite_data_provider = new SqliteDataProvider (this);
register_data_provider (sqlite_data_provider);
// Create the DataServer used to share data from the local DB
// on the local network.
data_server = new DataServer (this, data_provider);
/* Start the Server if required and subscribe to change in the settings.
* to adjust its behaviour. */
if (settings.get_boolean ("data-server-activated"))
data_server.start ();
settings.changed["data-server-activated"].connect ((settings, key) => {
if (settings.get_boolean ("data-server-activated"))
data_server.start ();
else
data_server.stop ();
});
/* Instantiate a NetworkDataProvider to communicate with other clinica on the
* network. */
networked_data_provider = new NetworkedDataProvider (this);
register_data_provider (networked_data_provider);
if (settings.get_string ("data-provider") == networked_data_provider.get_name ()) {
networked_data_provider.connect_to_server (
settings.get_string ("network-data-provider-host"),
int.parse (settings.get_string ("network-data-provider-port")));
// Check if the given authentication data is correct or not.
if (!networked_data_provider.try_authentication (
settings.get_string ("network-data-provider-host"),
settings.get_string ("network-data-provider-port"),
networked_data_provider.username, networked_data_provider.password)) {
// Check if the networked data provider is still the current one. If that's
// the case then disconnect and warn the user abount wrong credentials.
if (data_provider == networked_data_provider) {
var dialog = new MessageDialog (null, DialogFlags.MODAL, MessageType.ERROR, ButtonsType.OK,
"%s".printf (_("Username or password are wrong. Disconnecting from the server.")));
dialog.set_title (_("Error while authenticating on the server"));
dialog.run ();
dialog.destroy ();
select_data_provider (sqlite_data_provider);
networked_data_provider.disconnect_from_server ();
}
}
}
load_stores.begin ();
#if HAVE_PLUGINS
if (settings.get_boolean ("use-plugins"))
load_plugins.begin ();
#endif
}
#if HAVE_PLUGINS
private extern Peas.ExtensionSet setup_extension_set (PluginEngine engine);
/**
* @brief Instantiate the plugin engine and load
* all the plugins present in the directory
*/
public async void load_plugins () {
/* Setup plugin engine */
plugin_engine = new PluginEngine (this);
plugin_engine.enable_loader ("python");
unowned GLib.List<weak Peas.PluginInfo> list =
plugin_engine.get_plugin_list ();
string [] active_plugins = settings.get_active_plugins ();
foreach (Peas.PluginInfo info in list) {
if (info.get_module_name () in active_plugins) {
debug ("Activating plugin %s".printf (info.get_name ()));
plugin_engine.load_plugin (info);
}
else {
debug ("Plugin %s found but disabled".printf (info.get_name ()));
}
}
/* Setup the CoreActivatable extension set, so new DataProviders will be available */
core_set = setup_extension_set (plugin_engine);
}
#endif
private async void load_stores () {
/* Create the store that will be attached to the data provider selected
* above */
if (doctor_list_store != null)
return;
doctor_list_store = new DoctorListStore (this);
patient_list_store = new PatientListStore (this);
visit_list_store = new VisitListStore (this);
event_list_store = new EventListStore (this);
}
private async void reload_stores () {
// Reload the stores only if they are not already loaded.
debug ("Reloading stores");
if (doctor_list_store == null) {
load_stores.begin ();
return;
}
doctor_list_store.clear ();
doctor_list_store.load_data ();
patient_list_store.clear ();
patient_list_store.load_data ();
visit_list_store.clear ();
visit_list_store.load_data ();
event_list_store.clear ();
event_list_store.load_data ();
}
public void register_data_provider (DataProvider provider) {
data_providers.append (provider);
data_provider_added (provider);
if (data_provider == null)
data_provider = provider;
if (provider.get_name () == settings.get_string ("data-provider"))
data_provider = provider;
}
public void unregister_data_provider (DataProvider provider) {
if (provider == data_provider) {
data_provider = data_providers.nth_data (0);
settings.set_string ("data-provider", "");
}
}
internal void select_data_provider (DataProvider provider) {
if (data_provider == provider)
return;
data_provider = provider;
settings.set_string ("data-provider",
provider.get_name ());
reload_stores.begin ();
}
public void register_medicine_search_engine (MedicineSearchEngine engine) {
medicine_search_engines.append (engine);
/* If no medicine_search_engine is loaded, load this one */
if (settings.selected_search_engine == null) {
debug ("Loading search engine %s", engine.get_name ());
settings.selected_search_engine = engine;
}
medicine_search_engines_added (engine);
}
public bool unregister_medicine_search_engine (MedicineSearchEngine engine) {
if (engine == settings.selected_search_engine) {
settings.selected_search_engine = null;
settings.set_string ("medicine-search-engine", "");
}
foreach (MedicineSearchEngine e in medicine_search_engines) {
if (e == engine) {
medicine_search_engines.remove (e);
medicine_search_engines_removed (e);
return true;
}
}
return false;
}
public List<string> get_plugins_dirs () {
List<string> plugin_dirs = new List<string> ();
plugin_dirs.append (Path.build_filename (prefix, "lib", "clinica", "plugins"));
// Load custom dirs if defined in the appropriate Environment variable.
string user_defined_dirs = Environment.get_variable ("CLINICA_PLUGINS_PATH");
if (user_defined_dirs != null) {
foreach (var path in user_defined_dirs.split(":")) {
plugin_dirs.append (path);
}
}
return plugin_dirs;
}
public string get_data_path () {
return local_data_directory;
}
/**
* @brief Get the directory in which the FileStore can store its files.
*/
public string get_filestore_dir () {
return Path.build_filename (local_data_directory, "filestore");
}
}
}
|