Setting version number to 0.9.2
[urpo] / src / settingsdialog.cpp
1 /**************************************************************************
2
3     URPO
4
5     Unix Remote Printing Operation
6     Copyright (c) Arto Hyvättinen 2010
7
8     This file is part of URPO.
9
10     URPO is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation, either version 3 of the License, or
13     (at your option) any later version.
14
15     URPO is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18     GNU General Public License for more details.
19
20
21 **************************************************************************/
22
23 #include "settingsdialog.h"
24
25 #include <QLineEdit>
26 #include <QPushButton>
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QVBoxLayout>
30 #include <QFileDialog>
31 #include <QGroupBox>
32 #include "urpoconnectionsettings.h"
33
34 SettingsDialog::SettingsDialog(QWidget *parent) :
35     QDialog(parent)
36 {
37     settings_ = 0;
38     helpWidget_ = 0;
39
40
41     setWindowTitle(tr("Urpo Settings"));
42
43     serverEdit = new QLineEdit;
44     connect(serverEdit, SIGNAL(textChanged(QString)), this, SLOT(serverChanged(QString)));
45
46     userEdit = new QLineEdit;
47     identityEdit = new QLineEdit;
48
49     browseButton = new QPushButton(tr("Browse"));
50     connect(browseButton, SIGNAL(clicked()),this,SLOT(browse()));
51     okButton = new QPushButton(tr("Done"));
52     connect(okButton, SIGNAL(clicked()), this, SLOT(okay()) );
53     okButton->setEnabled(false);
54
55     helpButton = new QPushButton( tr("Help"));
56
57
58     QVBoxLayout* mainLayout = new QVBoxLayout;
59
60     QHBoxLayout* helpLayout = new QHBoxLayout;
61     helpLayout->addWidget( new QLabel(tr("Remote printing utility "
62                                       "via ssh and cups. "
63                                       "Please read help.")));
64     helpLayout->addWidget(helpButton);
65     QGroupBox* helpBox = new QGroupBox();
66     helpBox->setLayout(helpLayout);
67     helpBox->setFlat(false);
68     mainLayout->addWidget(helpBox);
69
70
71     QHBoxLayout* serverLayout = new QHBoxLayout;
72     serverLayout->addWidget( new QLabel(tr("Host name or ip address")));
73     serverLayout->addWidget( serverEdit );
74     mainLayout->addLayout(serverLayout);
75
76     QHBoxLayout* userLayout = new QHBoxLayout;
77     userLayout->addWidget( new QLabel(tr("Username")));
78     userLayout->addWidget( userEdit );
79     mainLayout->addLayout(userLayout);
80
81     QHBoxLayout* idLayout = new QHBoxLayout;
82     idLayout->addWidget( new QLabel( tr("Identity file")));
83     idLayout->addWidget(identityEdit);
84     idLayout->addWidget( browseButton);
85     mainLayout->addLayout(idLayout);
86
87     mainLayout->addStretch();
88
89     QHBoxLayout* okLayout = new QHBoxLayout;
90     okLayout->addStretch();
91     okLayout->addWidget(okButton);
92     mainLayout->addLayout(okLayout);
93
94     setLayout(mainLayout);
95
96 }
97
98
99 void SettingsDialog::setSettings(UrpoConnectionSettings *settings)
100 {
101     // Init dialog from UrpoConnectionSetting
102     settings_ = settings;
103     serverEdit->setText( settings_->getHost());
104     userEdit->setText(settings_->getUserid());
105     identityEdit->setText(settings_->getIdentity());
106
107 }
108
109 void SettingsDialog::setHelp(QTextBrowser *helpWidget)
110 {
111     // Set widget activated in help button
112     helpWidget_ = helpWidget;
113     connect( helpButton, SIGNAL(clicked()), this, SLOT(showHelp()));
114 }
115
116 void SettingsDialog::okay()
117 {
118     if( settings_ )
119     {
120         // Store settings
121         settings_->setHost( serverEdit->text());
122         settings_->setUserid( userEdit->text());
123         settings_->setIdentity( identityEdit->text());
124
125         settings_->store();
126
127         done(QDialog::Accepted);
128     }
129     else
130         done(QDialog::Rejected);
131 }
132
133 void SettingsDialog::showHelp()
134 {
135     // Activate help window
136     helpWidget_->home();
137     helpWidget_->show();
138     helpWidget_->raise();
139     helpWidget_->activateWindow();
140 }
141
142 void SettingsDialog::serverChanged(const QString& text)
143 {
144     // If there is a server setting, dialog will be accepted
145     // (if you have same userid in server and ssh key defined...)
146
147     okButton->setDisabled( text.isEmpty() );
148 }
149
150 void SettingsDialog::browse()
151 {
152     // 2010-08-13 default path -> /home/user (bug#6219)
153     QString path = QFileDialog::getOpenFileName(this, tr("Identity file"),"/home/user");
154     if(!path.isNull())
155         identityEdit->setText(path);
156 }