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
|
#include "preferences.h"
#include "mainwindow.h"
#include "models.h"
#include "divelocationmodel.h"
#include "prefs-macros.h"
#include "qthelper.h"
#include "subsurfacestartup.h"
#include <QSettings>
#include <QFileDialog>
#include <QMessageBox>
#include <QShortcut>
#include <QNetworkProxy>
#include <QNetworkCookieJar>
#include "subsurfacewebservices.h"
#if !defined(Q_OS_ANDROID) && defined(FBSUPPORT)
#include "socialnetworks.h"
#include <QWebView>
#endif
PreferencesDialog *PreferencesDialog::instance()
{
static PreferencesDialog *dialog = new PreferencesDialog(MainWindow::instance());
return dialog;
}
PreferencesDialog::PreferencesDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f)
{
ui.setupUi(this);
setAttribute(Qt::WA_QuitOnClose, false);
#if !defined(Q_OS_ANDROID) && defined(FBSUPPORT)
FacebookManager *fb = FacebookManager::instance();
facebookWebView = new QWebView(this);
ui.fbWebviewContainer->layout()->addWidget(facebookWebView);
if (fb->loggedIn()) {
facebookLoggedIn();
} else {
facebookDisconnect();
}
connect(facebookWebView, &QWebView::urlChanged, fb, &FacebookManager::tryLogin);
connect(fb, &FacebookManager::justLoggedIn, this, &PreferencesDialog::facebookLoggedIn);
connect(ui.fbDisconnect, &QPushButton::clicked, fb, &FacebookManager::logout);
connect(fb, &FacebookManager::justLoggedOut, this, &PreferencesDialog::facebookDisconnect);
#endif
connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
// connect(ui.defaultSetpoint, SIGNAL(valueChanged(double)), this, SLOT(defaultSetpointChanged(double)));
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
connect(close, SIGNAL(activated()), this, SLOT(close()));
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
connect(quit, SIGNAL(activated()), parent, SLOT(close()));
loadSettings();
setUiFromPrefs();
rememberPrefs();
}
void PreferencesDialog::facebookLoggedIn()
{
#if !defined(Q_OS_ANDROID) && defined(FBSUPPORT)
ui.fbDisconnect->show();
ui.fbWebviewContainer->hide();
ui.fbWebviewContainer->setEnabled(false);
ui.FBLabel->setText(tr("To disconnect Subsurface from your Facebook account, use the button below"));
#endif
}
void PreferencesDialog::facebookDisconnect()
{
#if !defined(Q_OS_ANDROID) && defined(FBSUPPORT)
// remove the connect/disconnect button
// and instead add the login view
ui.fbDisconnect->hide();
ui.fbWebviewContainer->show();
ui.fbWebviewContainer->setEnabled(true);
ui.FBLabel->setText(tr("To connect to Facebook, please log in. This enables Subsurface to publish dives to your timeline"));
if (facebookWebView) {
facebookWebView->page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
facebookWebView->setUrl(FacebookManager::instance()->connectUrl());
}
#endif
}
void PreferencesDialog::showEvent(QShowEvent *event)
{
setUiFromPrefs();
rememberPrefs();
QDialog::showEvent(event);
}
void PreferencesDialog::setUiFromPrefs()
{
}
void PreferencesDialog::restorePrefs()
{
prefs = oldPrefs;
setUiFromPrefs();
}
void PreferencesDialog::rememberPrefs()
{
oldPrefs = prefs;
}
void PreferencesDialog::syncSettings()
{
}
void PreferencesDialog::loadSettings()
{
// This code was on the mainwindow, it should belong nowhere, but since we didn't
// correctly fixed this code yet ( too much stuff on the code calling preferences )
// force this here.
loadPreferences();
QSettings s;
QVariant v;
}
void PreferencesDialog::buttonClicked(QAbstractButton *button)
{
switch (ui.buttonBox->standardButton(button)) {
case QDialogButtonBox::Discard:
restorePrefs();
syncSettings();
close();
break;
case QDialogButtonBox::Apply:
syncSettings();
break;
case QDialogButtonBox::FirstButton:
syncSettings();
close();
break;
default:
break; // ignore warnings.
}
}
#undef SB
void PreferencesDialog::on_resetSettings_clicked()
{
QSettings s;
QMessageBox response(this);
response.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
response.setDefaultButton(QMessageBox::Cancel);
response.setWindowTitle(tr("Warning"));
response.setText(tr("If you click OK, all settings of Subsurface will be reset to their default values. This will be applied immediately."));
response.setWindowModality(Qt::WindowModal);
int result = response.exec();
if (result == QMessageBox::Ok) {
copy_prefs(&default_prefs, &prefs);
setUiFromPrefs();
Q_FOREACH (QString key, s.allKeys()) {
s.remove(key);
}
syncSettings();
close();
}
}
void PreferencesDialog::emitSettingsChanged()
{
emit settingsChanged();
}
|