1 | /* |
2 | Copyright (c) 2008 Volker Krause <vkrause@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "serverconfigmodule.h" |
21 | |
22 | #include <kdebug.h> |
23 | #include <klocale.h> |
24 | #include <kpluginfactory.h> |
25 | #include <kpluginloader.h> |
26 | |
27 | #include <QSettings> |
28 | #include <QStackedWidget> |
29 | #include <QVBoxLayout> |
30 | |
31 | #include <akonadi/control.h> |
32 | #include <akonadi/servermanager.h> |
33 | #include <akonadi/private/xdgbasedirs_p.h> |
34 | |
35 | K_PLUGIN_FACTORY( ServerConfigModuleFactory, registerPlugin<ServerConfigModule>(); ) |
36 | K_EXPORT_PLUGIN( ServerConfigModuleFactory( "kcm_akonadi_server" ) ) |
37 | |
38 | using namespace Akonadi; |
39 | |
40 | ServerConfigModule::ServerConfigModule( QWidget * parent, const QVariantList & args ) : |
41 | KCModule( ServerConfigModuleFactory::componentData(), parent, args ) |
42 | { |
43 | KGlobal::locale()->insertCatalog( QLatin1String("kcm_akonadi" ) ); |
44 | KGlobal::locale()->insertCatalog( QLatin1String("libakonadi" ) ); |
45 | |
46 | QVBoxLayout *layout = new QVBoxLayout( this ); |
47 | setLayout( layout ); |
48 | |
49 | QWidget *storage_driver = new QWidget( this ); |
50 | layout->addWidget( storage_driver ); |
51 | ui_driver.setupUi( storage_driver ); |
52 | |
53 | m_stackWidget = new QStackedWidget( this ); |
54 | layout->addWidget( m_stackWidget ); |
55 | |
56 | // supported drivers |
57 | ui_driver.driverBox->addItem( QLatin1String("Mysql" ), QVariant( QLatin1String("QMYSQL" ) ) ); |
58 | ui_driver.driverBox->addItem( QLatin1String("PostgreSQL" ), QVariant( QLatin1String("QPSQL" ) ) ); |
59 | ui_driver.driverBox->addItem( QLatin1String("SQLite" ), QVariant( QLatin1String("QSQLITE3" ) ) ); |
60 | |
61 | setButtons( KCModule::Default | KCModule::Apply ); |
62 | |
63 | // MySQL |
64 | m_mysqlWidget = new QWidget( this ); |
65 | m_stackWidget->addWidget( m_mysqlWidget ); |
66 | ui_mysql.setupUi( m_mysqlWidget ); |
67 | |
68 | // PSQL |
69 | m_psqlWidget = new QWidget( this ); |
70 | m_stackWidget->addWidget( m_psqlWidget ); |
71 | ui_psql.setupUi( m_psqlWidget ); |
72 | |
73 | // SQLite |
74 | m_sqliteWidget = new QWidget( this ); |
75 | m_stackWidget->addWidget( m_sqliteWidget ); |
76 | // ui_sqlite.setupUi( m_sqliteWidget ); |
77 | |
78 | m_stackWidget->setCurrentWidget( m_mysqlWidget ); |
79 | |
80 | QWidget *widget = new QWidget( this ); |
81 | layout->addWidget( widget ); |
82 | ui.setupUi( widget ); |
83 | |
84 | connect( ui_mysql.startServer, SIGNAL(toggled(bool)), SLOT(changed()) ); |
85 | connect( ui_mysql.serverPath, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
86 | connect( ui_mysql.name, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
87 | connect( ui_mysql.host, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
88 | connect( ui_mysql.username, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
89 | connect( ui_mysql.password, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
90 | connect( ui_mysql.options, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
91 | |
92 | connect( ui_psql.startServer, SIGNAL(toggled(bool)), SLOT(changed()) ); |
93 | connect( ui_psql.name, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
94 | connect( ui_psql.host, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
95 | connect( ui_psql.username, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
96 | connect( ui_psql.password, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
97 | connect( ui_psql.port, SIGNAL(textChanged(QString)), SLOT(changed()) ); |
98 | connect( ui_psql.startServer, SIGNAL(toggled(bool)), ui_psql.messagewidget, SLOT(setHidden(bool))); |
99 | |
100 | connect( ui.startStopButton, SIGNAL(clicked()), SLOT(startStopClicked()) ); |
101 | connect( ui.restartButton, SIGNAL(clicked()), SLOT(restartClicked()) ); |
102 | connect( ui.selfTestButton, SIGNAL(clicked()), SLOT(selfTestClicked()) ); |
103 | |
104 | connect( ServerManager::self(), SIGNAL(started()), SLOT(updateStatus()) ); |
105 | connect( ServerManager::self(), SIGNAL(stopped()), SLOT(updateStatus()) ); |
106 | |
107 | connect( ui_driver.driverBox, SIGNAL(currentIndexChanged(int)), SLOT(driverChanged(int)) ); |
108 | connect( ui_driver.driverBox, SIGNAL(currentIndexChanged(int)), SLOT(changed()) ); |
109 | } |
110 | |
111 | void ServerConfigModule::load() |
112 | { |
113 | const QString serverConfigFile = XdgBaseDirs::akonadiServerConfigFile( XdgBaseDirs::ReadWrite ); |
114 | QSettings settings( serverConfigFile, QSettings::IniFormat ); |
115 | settings.beginGroup( QLatin1String("QMYSQL" ) ); |
116 | ui_mysql.startServer->setChecked( settings.value( QLatin1String("StartServer" ), true ).toBool() ); |
117 | ui_mysql.serverPath->setUrl( KUrl::fromPath( settings.value( QLatin1String("ServerPath" ), QString() ).toString() ) ); |
118 | ui_mysql.name->setText( settings.value( QLatin1String("Name" ), QLatin1String("akonadi" ) ).toString() ); |
119 | ui_mysql.host->setText( settings.value( QLatin1String("Host" ), QString() ).toString() ); |
120 | ui_mysql.username->setText( settings.value( QLatin1String("User" ), QString() ).toString() ); |
121 | ui_mysql.password->setText( settings.value( QLatin1String("Password" ), QString() ).toString() ); |
122 | ui_mysql.options->setText( settings.value( QLatin1String("Options" ), QString() ).toString() ); |
123 | settings.endGroup(); |
124 | |
125 | // postgresql group |
126 | settings.beginGroup( QLatin1String("QPSQL" ) ); |
127 | ui_psql.startServer->setChecked( settings.value( QLatin1String("StartServer" ), true ).toBool() ); |
128 | ui_psql.name->setText( settings.value( QLatin1String("Name" ), QLatin1String("akonadi" ) ).toString() ); |
129 | ui_psql.host->setText( settings.value( QLatin1String("Host" ), QString() ).toString() ); |
130 | ui_psql.username->setText( settings.value( QLatin1String("User" ), QString() ).toString() ); |
131 | ui_psql.password->setText( settings.value( QLatin1String("Password" ), QString() ).toString() ); |
132 | ui_psql.port->setText( settings.value( QLatin1String("Port" ), QLatin1String("5432" ) ).toString() ); |
133 | ui_psql.messagewidget->setVisible( !ui_psql.startServer->isChecked() ); |
134 | ui_psql.messagewidget->setCloseButtonVisible( false ); |
135 | ui_psql.messagewidget->setWordWrap( true ); |
136 | ui_psql.messagewidget->setMessageType( KMessageWidget::Information ); |
137 | ui_psql.messagewidget->setText( i18nc( "@info: special setting to configure" , |
138 | "Make sure you have %1 in your server postgres.conf " |
139 | "file before starting Akonadi." , QLatin1String( "<b>standard_conforming_strings = on</b>" ) ) ); |
140 | settings.endGroup(); |
141 | |
142 | // selected driver |
143 | settings.beginGroup( QLatin1String("GENERAL" ) ); |
144 | ui_driver.driverBox->setCurrentIndex( ui_driver.driverBox->findData( settings.value ( QLatin1String("Driver" ), QLatin1String("QMYSQL" ) ) ) ); |
145 | driverChanged( ui_driver.driverBox->currentIndex() ); |
146 | settings.endGroup(); |
147 | |
148 | updateStatus(); |
149 | } |
150 | |
151 | void ServerConfigModule::save() |
152 | { |
153 | kDebug(); |
154 | const QString serverConfigFile = XdgBaseDirs::akonadiServerConfigFile( XdgBaseDirs::ReadWrite ); |
155 | QSettings settings( serverConfigFile, QSettings::IniFormat ); |
156 | settings.beginGroup( QLatin1String("QMYSQL" ) ); |
157 | settings.setValue( QLatin1String("StartServer" ), ui_mysql.startServer->isChecked() ); |
158 | settings.setValue( QLatin1String("ServerPath" ), ui_mysql.serverPath->url().toLocalFile() ); |
159 | settings.setValue( QLatin1String("Name" ), ui_mysql.name->text() ); |
160 | settings.setValue( QLatin1String("Host" ), ui_mysql.host->text() ); |
161 | settings.setValue( QLatin1String("User" ), ui_mysql.username->text() ); |
162 | settings.setValue( QLatin1String("Password" ), ui_mysql.password->text() ); |
163 | settings.setValue( QLatin1String("Options" ), ui_mysql.options->text() ); |
164 | settings.endGroup(); |
165 | |
166 | // postgresql group |
167 | settings.beginGroup( QLatin1String("QPSQL" ) ); |
168 | settings.setValue( QLatin1String("StartServer" ), ui_psql.startServer->isChecked() ); |
169 | settings.setValue( QLatin1String("Name" ), ui_psql.name->text() ); |
170 | settings.setValue( QLatin1String("Host" ), ui_psql.host->text() ); |
171 | settings.setValue( QLatin1String("User" ), ui_psql.username->text() ); |
172 | settings.setValue( QLatin1String("Password" ), ui_psql.password->text() ); |
173 | settings.setValue( QLatin1String("Port" ), ui_psql.port->text() ); |
174 | settings.endGroup(); |
175 | |
176 | // sqlite group |
177 | settings.beginGroup( QLatin1String("SQLITE" ) ); |
178 | // TODO: make it configurable |
179 | settings.setValue( QLatin1String("Name" ), QLatin1String("akonadi" ) ); |
180 | settings.endGroup(); |
181 | |
182 | // selected driver |
183 | settings.beginGroup( QLatin1String("GENERAL" ) ); |
184 | settings.setValue( QLatin1String("Driver" ), ui_driver.driverBox->itemData( ui_driver.driverBox->currentIndex() ).toString() ); |
185 | settings.endGroup(); |
186 | settings.sync(); |
187 | |
188 | // TODO: restart server if needed |
189 | } |
190 | |
191 | void ServerConfigModule::defaults() |
192 | { |
193 | ui_mysql.startServer->setChecked( true ); |
194 | // TODO: detect default server path |
195 | ui_mysql.name->setText( QLatin1String("akonadi" ) ); |
196 | |
197 | ui_driver.driverBox->setCurrentIndex( ui_driver.driverBox->findData( QLatin1String("QMYSQL" ) ) ); |
198 | } |
199 | |
200 | void ServerConfigModule::updateStatus() |
201 | { |
202 | if ( ServerManager::isRunning() ) { |
203 | ui.statusLabel->setText( i18n( "<b>The Akonadi server is running.</b>" ) ); |
204 | ui.startStopButton->setText( i18n( "Stop" ) ); |
205 | ui.restartButton->setEnabled( true ); |
206 | } else { |
207 | ui.statusLabel->setText( i18n( "The Akonadi server is <b>not</b> running." ) ); |
208 | ui.startStopButton->setText( i18n( "Start" ) ); |
209 | ui.restartButton->setEnabled( false ); |
210 | } |
211 | } |
212 | |
213 | void ServerConfigModule::startStopClicked() |
214 | { |
215 | if ( ServerManager::isRunning() ) |
216 | Control::stop( this ); |
217 | else |
218 | Control::start( this ); |
219 | } |
220 | |
221 | void ServerConfigModule::restartClicked() |
222 | { |
223 | Control::restart( this ); |
224 | } |
225 | |
226 | void ServerConfigModule::selfTestClicked() |
227 | { |
228 | ServerManager::showSelfTestDialog( this ); |
229 | } |
230 | |
231 | void ServerConfigModule::driverChanged( int index ) |
232 | { |
233 | if ( ui_driver.driverBox->itemData( index ).toString() == QLatin1String("QMYSQL" ) ) { |
234 | m_stackWidget->setCurrentWidget( m_mysqlWidget ); |
235 | } else if ( ui_driver.driverBox->itemData( index ).toString() == QLatin1String("QPSQL" ) ) { |
236 | m_stackWidget->setCurrentWidget( m_psqlWidget ); |
237 | } else { |
238 | m_stackWidget->setCurrentWidget( m_sqliteWidget ); |
239 | } |
240 | } |
241 | |
242 | |