File: syncpreferences.cpp

package info (click to toggle)
nixnote2 2.0~beta11-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,448 kB
  • ctags: 7,058
  • sloc: cpp: 68,338; java: 1,096; sh: 834; makefile: 27
file content (186 lines) | stat: -rw-r--r-- 7,707 bytes parent folder | download
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
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2013 Randy Baumgarte

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 2
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 "syncpreferences.h"
#include "global.h"

#include <QGroupBox>

extern Global global;

SyncPreferences::SyncPreferences(QWidget *parent) :
    QWidget(parent)
{
    this->setFont(global.getGuiFont(font()));
    QGridLayout *mainLayout = new QGridLayout(this);
    setLayout(mainLayout);

    syncAutomatically = new QCheckBox(tr("Sync automatically"), this);
    syncAutomatically->setChecked(true);

    syncInterval = new QComboBox(this);
    syncInterval->addItem(tr("Every 15 minutes"), 15);
    syncInterval->addItem(tr("Every 30 minutes"), 30);
    syncInterval->addItem(tr("Every hour"), 60);
    syncInterval->addItem(tr("Every day"), 1440);

    syncOnStartup = new QCheckBox(tr("Sync on startup"), this);
    //syncOnStartup->setEnabled(false);
    syncOnShutdown = new QCheckBox(tr("Sync on shutdown"),this);
    //syncOnShutdown->setEnabled(false);
    enableSyncNotifications = new QCheckBox(tr("Enable sync notifications"), this);
    showGoodSyncMessagesInTray = new QCheckBox(tr("Show successful syncs"), this);
    apiRateRestart = new QCheckBox(tr("Restart sync on API limit (experimental)"), this);

    enableProxy = new QCheckBox(tr("Enable Proxy*"), this);
    enableSocks5 = new QCheckBox(tr("Enable Socks5"),this);
    QLabel *hostLabel = new QLabel(tr("Proxy Hostname"), this);
    QLabel *portLabel = new QLabel(tr("Proxy Port"), this);
    QLabel *userLabel = new QLabel(tr("Proxy Username"), this);
    QLabel *passwordLabel = new QLabel(tr("Proxy Password"),this);
    QLabel *restartLabel = new QLabel(tr("*Note: Restart required"),this);

    host = new QLineEdit(this);
    port = new QLineEdit(this);
    userId = new QLineEdit(this);
    password = new QLineEdit(this);

    enableProxy->setChecked(global.isProxyEnabled());
    enableSocks5->setChecked(global.isSocks5Enabled());
    host->setText(global.getProxyHost());
    port->setText(QString::number(global.getProxyPort()));
    port->setInputMask("00000");
    userId->setText(global.getProxyUserid());
    password->setText(global.getProxyPassword());
    password->setEchoMode(QLineEdit::Password);

    popupOnSyncError = new QCheckBox(tr("Popup message on sync errors."),0);
    popupOnSyncError->setChecked(global.popupOnSyncError());

    mainLayout->addWidget(enableSyncNotifications,0,0);
    mainLayout->addWidget(showGoodSyncMessagesInTray, 0,1);
    mainLayout->addWidget(syncOnStartup,1,0);
    mainLayout->addWidget(popupOnSyncError, 1,1);
    mainLayout->addWidget(syncOnShutdown,2,0);
    mainLayout->addWidget(syncAutomatically,3,0);
    mainLayout->addWidget(syncInterval, 3,1);
    mainLayout->addWidget(apiRateRestart, 4,0);

    mainLayout->addWidget(enableProxy,5,0);
    mainLayout->addWidget(enableSocks5,5,1);
    mainLayout->addWidget(hostLabel,6,0);
    mainLayout->addWidget(host, 6,1);
    mainLayout->addWidget(portLabel,7,0);
    mainLayout->addWidget(port,7,1);
    mainLayout->addWidget(userLabel, 8,0);
    mainLayout->addWidget(userId,8,1);
    mainLayout->addWidget(passwordLabel,9,0);
    mainLayout->addWidget(password,9,1);
    mainLayout->addWidget(restartLabel,10,0);
    mainLayout->setAlignment(Qt::AlignTop);

    global.settings->beginGroup("Sync");
    int interval = global.settings->value("syncInterval", 15).toInt();
    int index = syncInterval->findData(interval);
    syncInterval->setCurrentIndex(index);
    syncAutomatically->setChecked(global.settings->value("syncAutomatically", false).toBool());
    syncOnShutdown->setChecked(global.settings->value("syncOnShutdown", false).toBool());
    syncOnStartup->setChecked(global.settings->value("syncOnStartup", false).toBool());
    enableSyncNotifications->setChecked(global.settings->value("enableNotification", true).toBool());
    showGoodSyncMessagesInTray->setChecked(global.showGoodSyncMessagesInTray);
    apiRateRestart->setChecked(global.settings->value("apiRateLimitAutoRestart", false).toBool());
    global.settings->endGroup();
    global.showGoodSyncMessagesInTray = showGoodSyncMessagesInTray->isChecked();

    if (enableSyncNotifications->isChecked())
        showGoodSyncMessagesInTray->setEnabled(true);
    else
        showGoodSyncMessagesInTray->setEnabled(false);

    connect(syncAutomatically, SIGNAL(stateChanged(int)), this, SLOT(enableSyncStateChange()));
    connect(enableSyncNotifications, SIGNAL(toggled(bool)), this, SLOT(enableSuccessfulSyncMessagesInTray()));
    connect(enableProxy, SIGNAL(stateChanged(int)), this, SLOT(proxyCheckboxAltered(int)));
    if (!global.isProxyEnabled()) {
        proxyCheckboxAltered(Qt::Unchecked);
    }
}


SyncPreferences::~SyncPreferences() {
    delete syncInterval;
    delete syncOnShutdown;
    delete syncOnStartup;
    delete syncAutomatically;
}


void SyncPreferences::enableSyncStateChange() {
    if (syncAutomatically->isChecked())
        syncInterval->setEnabled(true);
    else
        syncInterval->setEnabled(false);
}

void SyncPreferences::enableSuccessfulSyncMessagesInTray() {
    if (this->enableSyncNotifications->isChecked())
        this->showGoodSyncMessagesInTray->setEnabled(true);
    else
        this->showGoodSyncMessagesInTray->setEnabled(false);
}

int SyncPreferences::getSyncInterval() {
    int index = syncInterval->currentIndex();
    return syncInterval->itemData(index).toInt();
}


void SyncPreferences::saveValues() {
    global.settings->beginGroup("Sync");
    global.settings->setValue("syncAutomatically", syncAutomatically->isChecked());
    global.settings->setValue("syncOnShutdown", syncOnShutdown->isChecked());
    global.settings->setValue("syncOnStartup", syncOnStartup->isChecked());
    global.settings->setValue("enableNotification", enableSyncNotifications->isChecked());
    global.settings->setValue("syncInterval", getSyncInterval());
    global.settings->setValue("showGoodSyncMessagesInTray", showGoodSyncMessagesInTray ->isChecked());
    global.settings->setValue("apiRateLimitAutoRestart", apiRateRestart ->isChecked());
    global.settings->endGroup();

    global.showGoodSyncMessagesInTray = showGoodSyncMessagesInTray->isChecked();

    global.setProxyEnabled(enableProxy->isChecked());
    global.setSocks5Enabled(enableSocks5->isChecked());
    global.setProxyHost(host->text().trimmed());
    global.setProxyPort(port->text().toInt());
    global.setProxyUserid(userId->text().trimmed());
    global.setProxyPassword(password->text().trimmed());
    global.setPopupOnSyncError(this->popupOnSyncError->isChecked());
}



void SyncPreferences::proxyCheckboxAltered(int state) {
    bool value = false;
    if (state == Qt::Checked)
        value = true;
    enableSocks5->setEnabled(value);
    host->setEnabled(value);
    port->setEnabled(value);
    userId->setEnabled(value);
    password->setEnabled(value);
}