2
This file is part of RoboJournal.
3
Copyright (c) 2012 by Will Kraft <pwizard@gmail.com>.
6
RoboJournal is free software: you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
11
RoboJournal is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with RoboJournal. If not, see <http://www.gnu.org/licenses/>.
21
#include "configmanager.h"
23
#include <ui_config.h>
30
#include <QErrorMessage>
31
#include <QMessageBox>
32
#include "newdatabase.h"
33
#include <QAbstractButton>
35
#include "ui_firstrun.h"
38
// get user home directory
39
QString homedir=QDir::homePath();
40
QChar separator=QDir::separator();
41
QString configdir=".robojournal";
45
QString Config::new_allow_root;
46
QString Config::new_entry_range;
47
QString Config::new_always_use_defaults;
48
QString Config::new_dbtype;
49
QString Config::new_default_db;
50
QString Config::new_default_host;
51
QString Config::new_default_port;
52
QString Config::new_default_user;
53
QString Config::new_show_all_entries;
54
QString Config::new_show_confirm;
55
QString Config::new_toolbar_pos;
56
QString Config::new_sort_by_day;
57
QString Config::new_alternate_rows;
58
QString Config::new_use_background;
59
QString Config::new_show_title;
60
QString Config::new_use_custom_theme;
61
QString Config::new_tile_bg;
62
QString Config::new_use_background_in_tree;
63
QString Config::new_text_hexcolor;
64
QString Config::new_font_face;
65
QString Config::new_background_image;
66
QString Config::new_font_size;
67
QString Config::new_record_time;
68
QString Config::new_use_local_time;
69
QString Config::new_24_hr;
70
QString Config::new_rich_text;
71
QString Config::new_date_mode;
72
QString Config::new_icon_labels;
75
//###################################################################################################
76
// Config home dir setup
77
QString configpath=homedir+separator+configdir;
81
QString config_name=homedir+separator+configdir+separator+"robojournal.cfg";
82
QFile configfile(config_name);
85
ConfigManager::ConfigManager()
89
// Function that loads config data into buffer on startup
91
void ConfigManager::ReadConfig(){
95
cout << "Output: Using home path: " + configpath.toStdString() << endl;
98
// Check for config file when program starts. If it's not there, rebuild it.
99
if(!configfile.exists()){
100
cout << "ERROR: Config file not found, rebuilding..." << endl;
102
// give the journal creator a way to know its the first run
103
Buffer::firstrun=true;
105
// Launch the firstrun tool
110
// Don't use the old RebuildConfig anymore now that the FirstRun tool is ready
114
//cout << "Done!" << endl;
117
cout << "OUTPUT: Reading data from config file robojournal.cfg...";
120
if (!configfile.open(QIODevice::ReadOnly | QIODevice::Text))
125
while(!configfile.atEnd()){
127
QString thisline=configfile.readLine();
128
//cout << thisline.toStdString();
133
var=thisline.remove(0,17);
134
int toolbar_pos=var.toInt();
135
Buffer::toolbar_pos=toolbar_pos;
139
var=thisline.remove(0, 18);
140
//cout << "Allow root: " << var.toStdString();
141
if(var.trimmed()=="1"){
142
Buffer::allowroot=true;
146
Buffer::allowroot=false;
153
var=thisline.remove(0, 17);
154
//cout << "Allow all entries: " << var.toStdString();
155
if(var.trimmed()=="0"){
156
Buffer::allentries=false;
160
Buffer::allentries=true;
166
var=thisline.remove(0, 20);
167
//cout << "Always use defaults: " << var.toStdString();
168
if(var.trimmed()=="0"){
169
Buffer::alwaysusedefaults=false;
170
//cout << "defaults are false";
173
Buffer::alwaysusedefaults=true;
174
//cout << "defaults are true";
180
var=thisline.remove(0, 8);
181
//cout << "Database type: " << var.toStdString();
182
Buffer::backend=var.trimmed();
186
var=thisline.remove(0, 14);
187
//cout << "Always warn: " << var.toStdString();
188
if(var.trimmed()=="0"){
189
Buffer::showwarnings=false;
190
//cout << "defaults are false";
193
Buffer::showwarnings=true;
194
//cout << "defaults are true";
199
var=thisline.remove(0, 12);
200
//cout << "Years to show: " << var.toStdString();
201
Buffer::entryrange=var.trimmed();
205
var=thisline.remove(0, 13);
206
//cout << "Default host: " << var.toStdString();
207
Buffer::defaulthost=var.trimmed();
212
var=thisline.remove(0, 11);
213
//cout << "Default database: " << var.toStdString();
214
Buffer::defaultdatabase=var.trimmed();
218
var=thisline.remove(0, 13);
219
//cout << "Default user: " << var.toStdString();
220
Buffer::defaultuser=var.trimmed();
224
var=thisline.remove(0,8);
225
Buffer::databaseport=var.trimmed();
230
var=thisline.remove(0,12);
231
//cout << "Sort by day: " << var.toStdString();
233
if(var.trimmed()=="0"){
234
Buffer::sortbyday=false;
237
Buffer::sortbyday=true;
241
// Use alternate colors
243
var=thisline.remove(0,17);
244
//cout << "Sort by day: " << var.toStdString();
246
if(var.trimmed()=="0"){
247
Buffer::alternate_rows=false;
250
Buffer::alternate_rows=true;
256
var=thisline.remove(0,15);
257
//cout << "Sort by day: " << var.toStdString();
259
if(var.trimmed()=="0"){
260
Buffer::use_background=false;
263
Buffer::use_background=true;
269
var=thisline.remove(0,10);
270
//cout << "Sort by day: " << var.toStdString();
272
if(var.trimmed()=="0"){
273
Buffer::use_custom_theme=false;
276
Buffer::use_custom_theme=true;
282
var=thisline.remove(0,16);
283
//cout << "Sort by day: " << var.toStdString();
285
if(var.trimmed()=="0"){
286
Buffer::tile_bg=false;
289
Buffer::tile_bg=true;
293
// Apply background to tree
295
var=thisline.remove(0,16);
296
//cout << "Sort by day: " << var.toStdString();
298
if(var.trimmed()=="0"){
299
Buffer::set_tree_background=false;
302
Buffer::set_tree_background=true;
308
var=thisline.remove(0,11);
309
//cout << "Sort by day: " << var.toStdString();
311
if(var.trimmed()=="NULL"){
312
Buffer::text_hexcolor.clear();
315
Buffer::text_hexcolor=var.trimmed();
321
var=thisline.remove(0,10);
322
//cout << "Sort by day: " << var.toStdString();
324
if(var.trimmed()=="NULL"){
325
Buffer::font_face.clear();
328
Buffer::font_face=var.trimmed();
334
var=thisline.remove(0,10);
335
//cout << "Sort by day: " << var.toStdString();
337
if(var.trimmed()=="NULL"){
338
Buffer::font_size="12";
341
Buffer::font_size=var.trimmed();
345
// Record time of entries
347
var=thisline.remove(0,9);
348
//cout << "Sort by day: " << var.toStdString();
350
if(var.trimmed()=="0"){
351
Buffer::keep_time=false;
354
Buffer::keep_time=true;
360
var=thisline.remove(0,11);
361
//cout << "Sort by day: " << var.toStdString();
363
if(var.trimmed()=="0"){
364
Buffer::use_system_time=false;
367
Buffer::use_system_time=true;
373
var=thisline.remove(0,11);
374
//cout << "Sort by day: " << var.toStdString();
376
if(var.trimmed()=="0"){
377
Buffer::show_title=false;
380
Buffer::show_title=true;
386
var=thisline.remove(0,8);
387
//cout << "Sort by day: " << var.toStdString();
389
if(var.trimmed()=="0"){
390
Buffer::use_24_hour=false;
393
Buffer::use_24_hour=true;
399
var=thisline.remove(0,12);
400
//cout << "Sort by day: " << var.toStdString();
402
Buffer::date_format=var.trimmed();
407
var=thisline.remove(0,10);
408
//cout << "Sort by day: " << var.toStdString();
410
if(var.trimmed()=="0"){
411
Buffer::rich_text=false;
414
Buffer::rich_text=true;
418
// Load background image
420
var=thisline.remove(0,17);
421
//cout << "Sort by day: " << var.toStdString();
423
Buffer::background_image=var.trimmed();
428
var=thisline.remove(0,12);
429
//cout << "Sort by day: " << var.toStdString();
431
if(var.trimmed()=="0"){
432
Buffer::show_icon_labels=false;
435
Buffer::show_icon_labels=true;
445
cout << "Done!" << endl;
446
cout << "OUTPUT: Stage 1 completed, proceeding to Stage 2..." << endl;
451
// Save changes made in Config class to config file
452
void ConfigManager::WriteConfig(){
456
// Function to rebuild config file if it is missing
457
void ::ConfigManager::RebuildConfig(){
460
directory.setPath(configpath);
463
if(!directory.exists()){
464
cout << "Rebuilding config file directory: " << configpath.toStdString() << endl;
465
directory.mkdir(configpath);
470
cout << "OUTPUT: Config directory already exists, proceeding...";
473
// once the directory exists, proceed to create the config file and write some default values to it
475
cout << "OUTPUT: Creating new config file: robojournal.cfg..." << endl;
476
cout << "OUTPUT: Populating robojournal.cfg with default values..." << endl;
477
QString sysuser=getenv("USERNAME");
478
if (configfile.open(QIODevice::ReadWrite)) {
479
QTextStream stream(&configfile);
480
stream << "ROBOJOURNAL CONFIGURATION v0.2" << endl;
481
stream << "#########################################" << endl;
482
stream << "TOOLBAR_LOCATION:1" << endl;
483
stream << "ALLOW_ROOT_LOGINS:0" << endl;
484
stream << "SHOW_ALL_ENTRIES:1" << endl;
485
stream << "ALWAYS_USE_DEFAULTS:1" << endl;
486
stream << "DB_TYPE:MySQL" << endl;
487
stream << "SHOW_WARNINGS:1" << endl;
488
stream << "ENTRY_RANGE:2" << endl;
489
stream << "DEFAULT_HOST:localhost" << endl;
490
stream << "DEFAULT_DB:"+sysuser+"_journal" << endl;
491
stream << "DEFAULT_USER:" + sysuser << endl; // use default system user name
492
stream << "DB_PORT:3306" << endl;
493
stream << "SORT_BY_DAY:0" << endl;
494
stream << "ALTERNATE_COLORS:1"<< endl;
495
stream << "USE_BACKGROUND:0"<< endl;
496
stream << "USE_THEME:0"<< endl;
497
stream << "TILE_BACKGROUND:1"<< endl;
498
stream << "BACKGROUND_TREE:0"<< endl;
499
stream << "FONT_COLOR:#000000"<< endl;
500
stream << "FONT_FACE:"<< endl;
501
stream << "FONT_SIZE:12"<< endl;
502
stream << "USE_TIME:1"<< endl;
503
stream << "LOCAL_TIME:1"<< endl;
504
stream << "SHOW_TITLE:1"<< endl;
505
stream << "24_HOUR:0"<< endl;
506
stream << "DATE_FORMAT:0"<< endl;
507
stream << "RICH_TEXT:0"<< endl;
508
stream << "BACKGROUND_IMAGE:NULL"<< endl;
509
stream << "ICON_LABELS:1"<< endl;
513
cout << "OUTPUT: Config file successfully replaced!" << endl;
514
cout << "OUTPUT: Attempting re-read of config file..." <<endl;
519
// This function updates the config file with new settings.
520
// It should always be invoked from the Config class, since the Config::$var
521
// static variables are assigned in the Config class.
523
void ConfigManager::UpdateConfig(){
526
// rewrite values in config file.
527
if(!configfile.isOpen()){
528
if (configfile.open(QIODevice::ReadWrite)) {
529
QTextStream stream(&configfile);
530
stream << "ROBOJOURNAL CONFIGURATION v0.2" << endl;
531
stream << "#########################################" << endl;
532
stream << "TOOLBAR_LOCATION:" << Config::new_toolbar_pos.trimmed() << endl;
533
stream << "ALLOW_ROOT_LOGINS:" << Config::new_allow_root.trimmed() << endl;
534
stream << "SHOW_ALL_ENTRIES:" << Config::new_show_all_entries.trimmed() << endl;
535
stream << "ALWAYS_USE_DEFAULTS:" << Config::new_always_use_defaults.trimmed() << endl;
536
stream << "DB_TYPE:" << Config::new_dbtype.trimmed() << endl;
537
stream << "SHOW_WARNINGS:" << Config::new_show_confirm.trimmed() << endl;
538
stream << "ENTRY_RANGE:" << Config::new_entry_range.trimmed() << endl;
539
stream << "DEFAULT_HOST:" << Config::new_default_host.trimmed() << endl;
540
stream << "DEFAULT_DB:" << Config::new_default_db.trimmed() << endl;
541
stream << "DEFAULT_USER:" << Config::new_default_user.trimmed() << endl;
542
stream << "DB_PORT:" << Config::new_default_port.trimmed() << endl;
543
stream << "SORT_BY_DAY:" << Config::new_sort_by_day.trimmed() << endl;
544
stream << "ALTERNATE_COLORS:"<< Config::new_alternate_rows.trimmed() << endl;
545
stream << "USE_BACKGROUND:"<< Config::new_use_background.trimmed() << endl;
546
stream << "USE_THEME:"<< Config::new_use_custom_theme.trimmed() << endl;
547
stream << "TILE_BACKGROUND:"<< Config::new_tile_bg.trimmed() << endl;
548
stream << "BACKGROUND_TREE:"<< Config::new_use_background_in_tree.trimmed() << endl;
549
stream << "FONT_COLOR:"<< Config::new_text_hexcolor.trimmed() << endl;
550
stream << "FONT_FACE:"<< Config::new_font_face.trimmed() << endl;
551
stream << "FONT_SIZE:"<< Config::new_font_size.trimmed() << endl;
552
stream << "USE_TIME:"<< Config::new_record_time.trimmed() << endl;
553
stream << "LOCAL_TIME:"<< Config::new_use_local_time.trimmed() << endl;
554
stream << "SHOW_TITLE:"<< Config::new_show_title.trimmed() << endl;
555
stream << "24_HOUR:"<< Config::new_24_hr.trimmed() << endl;
556
stream << "DATE_FORMAT:"<< Config::new_date_mode.trimmed() << endl;
557
stream << "RICH_TEXT:"<< Config::new_rich_text.trimmed() << endl;
558
stream << "BACKGROUND_IMAGE:" << Config::new_background_image.trimmed() << endl;
559
stream << "ICON_LABELS:" << Config::new_icon_labels.trimmed() << endl;
561
// Bugfix: always overwrite any junk data at the end of the config file with blank lines
562
stream << "" << endl;
563
stream << "" << endl;
564
stream << "" << endl;
565
stream << "" << endl;
569
// Replace old data in Buffer
570
ConfigManager::ReadConfig();
573
else{ // show error if robojournal.cfg is open
575
h.critical(NULL,"Error","RoboJournal is not able to modify the configuration because " +
576
config_name + " is in use by another process!");
582
CustomSetup() takes data from the Journal Creator class or the Journal Locator
583
class and uses it to create the config file. This function is supposed to replace
584
RebuildConfig() for most purposes. Now, RebuildConfig() only gets called if you're connecting to
585
an existing database at first run.
588
void ConfigManager::CustomSetup(QString host,QString db_name, QString port, QString newuser){
591
directory.setPath(configpath);
594
if(!directory.exists()){
595
cout << "Rebuilding config file directory: " << configpath.toStdString() << endl;
596
directory.mkdir(configpath);
601
cout << "OUTPUT: Config directory already exists, proceeding...";
604
// once the directory exists, proceed to create the config file and write some default values to it
606
cout << "OUTPUT: Creating new config file: robojournal.cfg..." << endl;
607
cout << "OUTPUT: Populating robojournal.cfg with values from setup form..." << endl;
609
if (configfile.open(QIODevice::ReadWrite)) {
610
QTextStream stream(&configfile);
611
stream << "ROBOJOURNAL CONFIGURATION v0.2" << endl;
612
stream << "#########################################" << endl;
613
stream << "TOOLBAR_LOCATION:1" << endl;
614
stream << "ALLOW_ROOT_LOGINS:0" << endl;
615
stream << "SHOW_ALL_ENTRIES:1" << endl;
616
stream << "ALWAYS_USE_DEFAULTS:1" << endl;
617
stream << "DB_TYPE:MySQL" << endl;
618
stream << "SHOW_WARNINGS:1" << endl;
619
stream << "ENTRY_RANGE:2" << endl;
620
stream << "DEFAULT_HOST:" << host << endl;
621
stream << "DEFAULT_DB:"<< db_name << endl;
622
stream << "DEFAULT_USER:" + newuser << endl; // use default system user name
623
stream << "DB_PORT:" << port << endl;
624
stream << "SORT_BY_DAY:0" << endl;
625
stream << "ALTERNATE_COLORS:1"<< endl;
626
stream << "USE_BACKGROUND:0"<< endl;
627
stream << "USE_THEME:0"<< endl;
628
stream << "TILE_BACKGROUND:1"<< endl;
629
stream << "BACKGROUND_TREE:0"<< endl;
630
stream << "FONT_COLOR:#000000"<< endl;
631
stream << "FONT_FACE:"<< endl;
632
stream << "FONT_SIZE:12"<< endl;
633
stream << "USE_TIME:1"<< endl;
634
stream << "LOCAL_TIME:1"<< endl;
635
stream << "SHOW_TITLE:1"<< endl;
636
stream << "24_HOUR:0"<< endl;
637
stream << "DATE_FORMAT:0"<< endl;
638
stream << "RICH_TEXT:0"<< endl;
639
stream << "BACKGROUND_IMAGE:NULL"<< endl;
640
stream << "ICON_LABELS:1"<< endl;
643
cout << "OUTPUT: Config file successfully replaced!" << endl;
644
cout << "OUTPUT: Attempting re-read of config file..." <<endl;