a5a0d16b33a58ef836d2ca2011ba1c37c29ebfe6
[irwi] / src / settingsdlg.cpp
1 #include "settingsdlg.h"
2 #include "advsettingsdlg.h"
3 #include "selectremotedlg.h"
4 #include "aboutdlg.h"
5
6 #include <QHBoxLayout>
7 #include <QVBoxLayout>
8 #include <QWidget>
9 #include <QDialog>
10 #include <QPushButton>
11 #include <QLabel>
12 #include <QDebug>
13 #include <QNetworkConfiguration>
14
15 SettingsDlg::SettingsDlg(QWidget *parent)
16     : QDialog(parent)
17 {
18     QSettings settings(this);
19     m_layout = new QVBoxLayout(this);
20     m_btnLayout = new QHBoxLayout();
21     m_remoteNameLayout = new QHBoxLayout();
22     
23     m_advSettingsBtn = new QPushButton(tr("Advanced"), this);
24     m_selectRemoteBtn = new QPushButton(tr("Select remote"), this);
25     m_aboutBtn = new QPushButton(tr("About"), this);
26     m_rateUpBtn = new QPushButton(
27             QIcon(settings.value("rateUpIcon",
28                 "/usr/share/icons/hicolor/48x48/hildon/chat_smiley_happy.png").
29                 toString()),
30             "", this);
31     m_rateDownBtn = new QPushButton(
32             QIcon(settings.value("rateDownIcon",
33                 "/usr/share/icons/hicolor/48x48/hildon/chat_smiley_sad.png").
34                 toString()),
35             "", this);
36     m_rateUpBtn->setMaximumSize(72, 72);
37     m_rateDownBtn->setMaximumSize(72, 72);
38
39     m_btnLayout->addWidget(m_advSettingsBtn);
40     m_btnLayout->addWidget(m_selectRemoteBtn);
41     m_btnLayout->addWidget(m_aboutBtn);
42
43     m_remoteNameLabel = new QLabel(this);
44     m_ratingLabel = new QLabel(this);
45     m_remoteNameLayout->addWidget(m_remoteNameLabel);
46     m_remoteNameLayout->addWidget(m_ratingLabel);
47     m_remoteNameLayout->addWidget(m_rateUpBtn);
48     m_remoteNameLayout->addWidget(m_rateDownBtn);
49
50     connect(m_advSettingsBtn, SIGNAL(clicked()),
51             this, SLOT(showAdvSettingsDlg()));
52     connect(m_selectRemoteBtn, SIGNAL(clicked()),
53             this, SLOT(showSelectRemoteDlg()));
54     connect(m_aboutBtn, SIGNAL(clicked()),
55             this, SLOT(showAboutDlg()));
56     connect(m_rateUpBtn, SIGNAL(clicked()),
57             this, SLOT(rateUpClicked()));
58     connect(m_rateDownBtn, SIGNAL(clicked()),
59             this, SLOT(rateDownClicked()));
60  
61     m_layout->addLayout(m_remoteNameLayout);
62     m_layout->addLayout(m_btnLayout);
63     this->setLayout(m_layout);
64
65     QString selectedRemote = settings.value("remoteName", "").toString();
66     if (selectedRemote == "") {
67         m_remoteNameLabel->setText("No remote selected");
68         enableRateBtns(false);
69     } else {
70         // Create remote by name and update it's info if online
71         m_remote = Remote(selectedRemote);
72         m_remoteNameLabel->setText(selectedRemote);
73         m_netConfMan = new QTM_PREPEND_NAMESPACE(
74                 QNetworkConfigurationManager)(this);
75         connect(m_netConfMan, SIGNAL(updateCompleted()),
76                 this, SLOT(onNetworkStatusUpdate()));
77         m_netConfMan->updateConfigurations();
78         enableRateBtns(false);
79     }
80 }
81
82 SettingsDlg::~SettingsDlg()
83 {
84     delete m_advSettingsBtn;
85     delete m_selectRemoteBtn;
86     delete m_rateUpBtn;
87     delete m_rateDownBtn;
88     delete m_aboutBtn;
89     delete m_remoteNameLabel;
90     delete m_ratingLabel;
91     delete m_btnLayout;
92     delete m_remoteNameLayout;
93     delete m_layout;
94     delete m_netConfMan;
95 }
96
97 void SettingsDlg::setBusy(bool busy)
98 {
99     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, busy);
100     setEnabled(!busy);
101 }
102
103 void SettingsDlg::showAdvSettingsDlg()
104 {
105     AdvSettingsDlg dlg;
106     dlg.exec();
107 }
108
109 void SettingsDlg::showSelectRemoteDlg()
110 {
111     SelectRemoteDlg dlg;
112     connect(&dlg, SIGNAL(remoteChanged(Remote)),
113             this, SLOT(setRemote(Remote)));
114     dlg.exec();
115 }
116
117 void SettingsDlg::showAboutDlg()
118 {
119     AboutDlg dlg;
120     dlg.exec();
121 }
122
123 void SettingsDlg::setRemote(Remote r)
124 {
125     m_remote = r;
126     updateRemoteInfo();
127     enableRateBtns();
128 }
129
130 void SettingsDlg::onNetworkStatusUpdate()
131 {
132     if (m_netConfMan->isOnline()) {
133         setBusy();
134         connect(&m_remote, SIGNAL(infoUpdated()),
135                 this, SLOT(updateRemoteInfo()));
136         m_remote.updateInfo();
137         enableRateBtns();
138     }
139 }
140
141 void SettingsDlg::updateRemoteInfo()
142 {
143     setBusy(false);
144     m_remoteNameLabel->setText(m_remote.mfg() + " " + m_remote.name());
145     m_ratingLabel->setText(tr("Rating") + ": "
146             + QString::number(m_remote.rating()));
147 }
148
149 void SettingsDlg::rateUpClicked()
150 {
151     m_remote.sendRating(Rating::Up);
152     processRatingSent();
153 }
154
155 void SettingsDlg::rateDownClicked()
156 {
157     m_remote.sendRating(Rating::Down);
158     processRatingSent();
159 }
160
161 void SettingsDlg::processRatingSent()
162 {
163     enableRateBtns(false);
164     m_remote.updateInfo();
165 }
166
167 void SettingsDlg::enableRateBtns(bool enable)
168 {
169     m_rateUpBtn->setEnabled(enable);
170     m_rateDownBtn->setEnabled(enable);
171 }
172
173