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