Maemo package folder added.
[speedfreak] / Client / profiledialog.cpp
1 /*
2  * Profile dialog class
3  *
4  * @author     Toni Jussila <toni.jussila@fudeco.com>
5  * @copyright  (c) 2010 Speed Freak team
6  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include <QString>
10 #include <QFileDialog>
11 #include <QImage>
12 #include <QMessageBox>
13 #include <QPixmap>
14 #include <QSize>
15 #include <QDebug>
16 #include "profiledialog.h"
17 #include "ui_profiledialog.h"
18 #include "usersettings.h"
19 #include "settingsdialog.h"
20 #include "xmlreader.h"
21
22 /**
23   * Constructor of this class.
24   *
25   * @param QWidget pointer to parent object. By default the value is NULL.
26   */
27 ProfileDialog::ProfileDialog(SettingsDialog *parent) : QDialog(parent), ui(new Ui::ProfileDialog)
28 {
29     qDebug() << "__ProfileDialog";
30     ui->setupUi(this);
31
32     if (loginSaved())
33     {
34         this->setWindowTitle("Profile - " + parent->getUserName());
35     }
36     else
37     {
38         this->setWindowTitle("Profile");
39     }
40
41     // User info label
42     ui->labelInfoToUser->setVisible(0);
43     ui->labelInfoToUser->setText("");
44
45     // Button
46     imageButtonState = false;
47
48     manufacturer="";
49     type="";
50     model="";
51     description="";
52     picture="";
53
54     // Read user profile xml
55     QString filename = "/home/user/MyDocs/speedfreak/profile/" + parent->getUserName() + "_profile.xml";
56     QFile file(filename);
57
58     if (!file.open(QFile::ReadOnly))
59     {
60         qDebug() << "_xmlRead fail";
61         return;
62     }
63     else
64     {
65         xmlReader = new XmlReader();
66         xmlReader->xmlReadProfile(&file,this);
67     }
68     file.close();
69 }
70
71 /**
72   * Destructor of this class. Deletes all dynamic objects and sets them to NULL.
73   */
74 ProfileDialog::~ProfileDialog()
75 {
76     qDebug() << "__~ProfileDialog";
77
78     if(ui)
79         delete ui;
80
81     if(xmlReader)
82         delete xmlReader;
83 }
84
85 /**
86   *
87   */
88 void ProfileDialog::changeEvent(QEvent *e)
89 {
90     QDialog::changeEvent(e);
91     switch (e->type()) {
92     case QEvent::LanguageChange:
93         ui->retranslateUi(this);
94         break;
95     default:
96         break;
97     }
98 }
99
100 /**
101   * This slot function called when image button clicked.
102   *
103   */
104 void ProfileDialog::on_buttonImage_clicked()
105 {
106     if (imageButtonState == false)
107     {
108         ui->buttonImage->setText("Load image");
109     }
110     else
111     {
112         ui->buttonImage->setText("Change image");
113     }
114
115     QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
116     loadPicture(fileName);
117 }
118
119 /**
120   * This function load picture.
121   *
122   * @param QString file name
123   */
124 void ProfileDialog::loadPicture(QString fileName)
125 {
126     if (!fileName.isEmpty())
127     {
128         QImage image(fileName);
129         if (image.isNull())
130         {
131             QMessageBox::information(this, tr("Profile"),tr("Cannot load %1.").arg(fileName));
132             return;
133         }
134         ui->labelImage->setPixmap(QPixmap::fromImage(image.scaled(QSize(120,120), Qt::KeepAspectRatio)));
135         picture = fileName;
136     }
137 }
138
139 /**
140   * This slot function called when save button clicked.
141   *
142   * @todo server connection
143   */
144 void ProfileDialog::on_buttonSave_clicked()
145 {
146     // Save labels data
147     setManufacturer(ui->lineEditManufacturer->text());
148     setType(ui->lineEditType->text());
149     setModel(ui->lineEditModel->text());
150     setDescription(ui->textEditDescription->toPlainText());
151
152     this->close();
153     // emit settingsdialog --> mainwindow --> httpclient
154     //emit saveprofile();
155 }
156
157 /**
158   * Get function return manufacturer.
159   *
160   * @return QString manufacturer
161   */
162 QString ProfileDialog::getManufacturer()
163 {
164     return manufacturer;
165 }
166
167 /**
168   * Get function return type.
169   *
170   * @return QString type
171   */
172 QString ProfileDialog::getType()
173 {
174     return type;
175 }
176
177 /**
178   * Get function return model.
179   *
180   * @return QString model
181   */
182 QString ProfileDialog::getModel()
183 {
184     return model;
185 }
186
187 /**
188   * Get function return description.
189   *
190   * @return QString description
191   */
192 QString ProfileDialog::getDescription()
193 {
194     return description;
195 }
196
197 /**
198   * Get function return picture.
199   *
200   * @return QString picture
201   */
202 QString ProfileDialog::getPicture()
203 {
204     return picture;
205 }
206
207 /**
208   * This function set manufacturer.
209   *
210   * @param QString manufacturer
211   */
212 void ProfileDialog::setManufacturer(QString m)
213 {
214     manufacturer = m;
215     ui->lineEditManufacturer->setText(m);
216 }
217
218 /**
219   * This function set type.
220   *
221   * @param QString type
222   */
223 void ProfileDialog::setType(QString t)
224 {
225     type = t;
226     ui->lineEditType->setText(t);
227 }
228
229 /**
230   * This function set model.
231   *
232   * @param QString model
233   */
234 void ProfileDialog::setModel(QString m)
235 {
236     model = m;
237     ui->lineEditModel->setText(m);
238 }
239
240 /**
241   * This function set description.
242   *
243   * @param QString description
244   */
245 void ProfileDialog::setDescription(QString d)
246 {
247     description = d;
248     ui->textEditDescription->setText(d);
249 }
250
251 /**
252   * This function set description.
253   *
254   * @param QString picture
255   */
256 void ProfileDialog::setPicture(QString p)
257 {
258     picture = p;
259     loadPicture(p);
260 }
261
262 /**
263   * This function set label info text to user.
264   *
265   * @param QString info text
266   */
267 void ProfileDialog::setLabelInfoToUser(QString infoText)
268 {
269     ui->labelInfoToUser->setVisible(1);
270     this->ui->labelInfoToUser->setText(infoText);
271 }