0.7.1
[fapman] / settings.cpp
1 /*
2         This file is part of Faster Application Manager.
3
4         Faster Application Manager is free software: you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation, either version 3 of the License, or
7         (at your option) any later version.
8
9         Faster Application Manager is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with Faster Application Manager.  If not, see <http://www.gnu.org/licenses/>.
16
17         (C) Heikki Holstila 2010
18 */
19
20 #include <QtNetwork>
21 #include "settings.h"
22 #include "ui_settings.h"
23 #include "mainwindow.h"
24 #include "aaptinterface.h"
25 #include "packageview.h"
26 #include "confirmdialog.h"
27 #include "searchoptions.h"
28
29 Settings::Settings(QWidget *parent) :
30     QDialog(parent),
31     ui(new Ui::Settings)
32 {
33     ui->setupUi(this);
34
35         iAptInterface = 0;
36         iQSettings = new QSettings("/root/.fapman/settings.ini",QSettings::IniFormat);
37 }
38
39 Settings::~Settings()
40 {
41         iQSettings->sync();
42         delete iQSettings; iQSettings=0;
43         delete ui; ui=0;
44 }
45
46 void Settings::changeEvent(QEvent *e)
47 {
48     QDialog::changeEvent(e);
49     switch (e->type()) {
50     case QEvent::LanguageChange:
51         ui->retranslateUi(this);
52         break;
53     default:
54         break;
55     }
56 }
57
58 void Settings::openWin()
59 {
60         ui->checkBox_fetchdates->setChecked( iQSettings->value("fetch_dates", false).toBool() );
61         ui->checkBox_autorotation->setChecked( iQSettings->value("disable_autorotation", false).toBool() );
62         ui->checkBox_autoremove->setChecked( iQSettings->value("enable_autoremove", true).toBool() );
63         ui->checkBox_autoclean->setChecked( iQSettings->value("enable_autoclean", true).toBool() );
64         ui->checkBox_remove_readfull->setChecked( iQSettings->value("remove_readfull", false).toBool() );
65         ui->checkBox_no_catalogs_autoupdate->setChecked( iQSettings->value("no_catalogs_autoupdate",false).toBool() );
66         ui->checkBox_always_fetch_changes->setChecked( iQSettings->value("always_fetch_changes",false).toBool() );
67
68         ui->checkBox_proxies->setChecked( iQSettings->value("use_proxies", false).toBool() );
69         ui->lineEdit_http_proxy->setText( iQSettings->value("http_proxy","").toString() );
70         ui->lineEdit_https_proxy->setText( iQSettings->value("https_proxy","").toString() );
71
72         ui->comboBox_sortorder->setCurrentIndex( iQSettings->value("default_sort_order",0).toInt() );
73
74         ui->checkBox_playsound->setChecked( iQSettings->value("sound_notify", false).toBool() );
75         ui->lineEdit_soundfile->setText( iQSettings->value("sound_file","/usr/share/sounds/ui-operation_ready.wav").toString() );
76
77         open();
78 }
79
80 void Settings::on_btn_OK_clicked()
81 {
82         if( ui->checkBox_fetchdates->isChecked() != iQSettings->value("fetch_dates",false).toBool() && iAptInterface ) {
83                 iAptInterface->setNeedRefresh(-1,-1,-1,1);
84         }
85         if( ui->comboBox_sortorder->currentIndex() != iQSettings->value("default_sort_order",0).toInt() ) {
86                 iPackageView->setSortOrder( (PackageView::sortOrder)ui->comboBox_sortorder->currentIndex() );
87         }
88
89         iQSettings->setValue("fetch_dates", ui->checkBox_fetchdates->isChecked() );
90         iQSettings->setValue("disable_autorotation", ui->checkBox_autorotation->isChecked() );
91         iQSettings->setValue("enable_autoremove", ui->checkBox_autoremove->isChecked() );
92         iQSettings->setValue("enable_autoclean", ui->checkBox_autoclean->isChecked() );
93         iQSettings->setValue("remove_readfull", ui->checkBox_remove_readfull->isChecked() );
94         iQSettings->setValue("no_catalogs_autoupdate", ui->checkBox_no_catalogs_autoupdate->isChecked() );
95         iQSettings->setValue("always_fetch_changes", ui->checkBox_always_fetch_changes->isChecked() );
96
97         iQSettings->setValue("use_proxies", ui->checkBox_proxies->isChecked() );
98         iQSettings->setValue("http_proxy", ui->lineEdit_http_proxy->text().trimmed() );
99         iQSettings->setValue("https_proxy", ui->lineEdit_https_proxy->text().trimmed() );
100
101         iQSettings->setValue("default_sort_order", ui->comboBox_sortorder->currentIndex() );
102
103         iQSettings->setValue("sound_notify", ui->checkBox_playsound->isChecked() );
104         iQSettings->setValue("sound_file", ui->lineEdit_soundfile->text() );
105
106         if( ui->comboBox_sortorder->currentIndex() == (int)PackageView::SortDateDesc && !ui->checkBox_fetchdates->isChecked() ) {
107                 iQSettings->setValue("fetch_dates", true );
108                 iAptInterface->setNeedRefresh(-1,-1,-1,1);
109
110                 ConfirmDialog d(false, this);
111                 d.setText("Notice","You have selected date as the default sorting criterion. Fetching dates has been automatically enabled.");
112                 d.exec();
113         }
114
115 #ifdef Q_WS_MAEMO_5
116         if( !iQSettings->value("disable_autorotation",false).toBool() ) {
117                 dynamic_cast<MainWindow*>(this->parent())->setAttribute(Qt::WA_Maemo5AutoOrientation);
118         } else {
119                 dynamic_cast<MainWindow*>(this->parent())->setAttribute(Qt::WA_Maemo5LandscapeOrientation);
120         }
121 #endif
122
123         accept();
124 }
125
126 void Settings::on_pushButton_picksound_clicked()
127 {
128         //QFileInfo i( ui->lineEdit_soundfile->text() );
129         QString f = QFileDialog::getOpenFileName(this, "Select sound file", "/", "Audio (*.wav *.mp3 *.ogg)");
130         if( !f.isNull() ) {
131                 ui->lineEdit_soundfile->setText(f);
132         }
133 }
134
135 void Settings::on_pushButton_searchOptions_clicked()
136 {
137         SearchOptions s(this);
138         s.setSelections( iQSettings->value("search_pkgnames",true).toBool(),
139                                          iQSettings->value("search_displaynames",true).toBool(),
140                                          iQSettings->value("search_descshort",true).toBool(),
141                                          iQSettings->value("search_desclong",false).toBool() );
142         if( s.exec() )
143         {
144                 iQSettings->setValue("search_pkgnames", s.searchPkgName() );
145                 iQSettings->setValue("search_displaynames", s.searchDisplayName() );
146                 iQSettings->setValue("search_descshort", s.searchDescShort() );
147                 iQSettings->setValue("search_desclong", s.searchDescLong() );
148
149                 iPackageView->setSearchOptions(s.searchPkgName(), s.searchDisplayName(),
150                                                                            s.searchDescShort(), s.searchDescLong() );
151         }
152 }
153
154 QNetworkProxy Settings::createProxyFromString(QString proxystr)
155 {
156         proxystr = proxystr.trimmed();
157         QString hostname = "";
158         quint16 port = 8080;
159         QString user;
160         QString pass;
161
162         QString proto;
163         int pos_proto = proxystr.indexOf("://");
164         if( pos_proto != -1 )
165         {
166                 proto = proxystr.left(pos_proto+3);
167                 proxystr = proxystr.mid(pos_proto+3);
168         }
169
170         QString temp;
171         QString temp2;
172         int pos_half = proxystr.indexOf('@');
173         if( pos_half==-1 ) {
174                 temp = proxystr;
175         } else {
176                 temp = proxystr.mid(pos_half+1);
177                 temp2 = proxystr.left(pos_half);
178         }
179
180         if( !temp2.isNull() ) {
181                 int pos_pass = temp2.indexOf(':');
182                 if( pos_pass==-1 ) {
183                         user = temp2;
184                 } else {
185                         user = temp2.left(pos_pass);
186                         pass = temp2.mid(pos_pass+1);
187                 }
188         }
189
190         int pos_port = temp.indexOf(':');
191         if( pos_port==-1 ) {
192                 hostname = temp;
193         } else {
194                 hostname = temp.left(pos_port);
195                 port = temp.mid(pos_port+1).remove('/').toInt();
196         }
197
198         hostname = proto + hostname;
199         qDebug() << "creating proxy" << hostname << port << user << pass;
200
201         QNetworkProxy p(QNetworkProxy::HttpProxy, hostname, port, user, pass);
202         return p;
203 }