configdialog.cpp 6.22 KB
Newer Older
1
2
/*
    Copyright (c) 2009 Grégory Oestreicher <greg@kamago.net>
3

4
5
6
7
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
8

9
10
11
12
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
13

14
15
16
17
18
19
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "configdialog.h"
20
#include "davutils.h"
21
#include "settings.h"
22
#include "urlconfigurationdialog.h"
23
24

#include <kconfigdialogmanager.h>
25
26
27
28
#include <kconfigskeleton.h>
#include <klocale.h>

#include <QtCore/QList>
Grgory Oestreicher's avatar
Grgory Oestreicher committed
29
#include <QtCore/QPointer>
30
31
32
#include <QtCore/QStringList>
#include <QtGui/QStandardItem>
#include <QtGui/QStandardItemModel>
33

Tobias Koenig's avatar
Tobias Koenig committed
34
35
ConfigDialog::ConfigDialog( QWidget *parent )
  : KDialog( parent )
36
{
37
  mUi.setupUi( mainWidget() );
38

39
40
41
42
  mModel = new QStandardItemModel();
  QStringList headers;
  headers << i18n( "Protocol" ) << i18n( "URL" );
  mModel->setHorizontalHeaderLabels( headers );
43

44
  mUi.configuredUrls->setModel( mModel );
45
  mUi.configuredUrls->setRootIsDecorated( false );
46

47
48
49
50
51
  foreach ( const DavUtils::DavUrl &url, Settings::self()->configuredDavUrls() ) {
    KUrl displayUrl = url.url();
    displayUrl.setUser( QString() );
    addModelRow( DavUtils::protocolName( url.protocol() ), displayUrl.prettyUrl() );
  }
52

53
54
  mManager = new KConfigDialogManager( this, Settings::self() );
  mManager->updateWidgets();
55

Tobias Koenig's avatar
Tobias Koenig committed
56
57
  connect( mUi.kcfg_displayName, SIGNAL( textChanged( const QString& ) ), this, SLOT( checkUserInput() ) );
  connect( mUi.configuredUrls->selectionModel(), SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ),
58
           this, SLOT( checkConfiguredUrlsButtonsState() ) );
59

60
61
62
  connect( mUi.addButton, SIGNAL( clicked() ), this, SLOT( onAddButtonClicked() ) );
  connect( mUi.removeButton, SIGNAL( clicked() ), this, SLOT( onRemoveButtonClicked() ) );
  connect( mUi.editButton, SIGNAL( clicked() ), this, SLOT( onEditButtonClicked() ) );
63

64
65
  connect( this, SIGNAL( okClicked() ), this, SLOT( onOkClicked() ) );
  connect( this, SIGNAL( cancelClicked() ), this, SLOT( onCancelClicked() ) );
66

Tobias Koenig's avatar
Tobias Koenig committed
67
  checkUserInput();
68
69
70
71
72
73
}

ConfigDialog::~ConfigDialog()
{
}

74
75
void ConfigDialog::checkUserInput()
{
Tobias Koenig's avatar
Tobias Koenig committed
76
  checkConfiguredUrlsButtonsState();
77

Tobias Koenig's avatar
Tobias Koenig committed
78
  if ( !mUi.kcfg_displayName->text().isEmpty() && !( mModel->invisibleRootItem()->rowCount() == 0 ) )
Tobias Koenig's avatar
Tobias Koenig committed
79
    enableButtonOk( true );
80
  else
Tobias Koenig's avatar
Tobias Koenig committed
81
    enableButtonOk( false );
82
83
84
85
}

void ConfigDialog::onAddButtonClicked()
{
Grgory Oestreicher's avatar
Grgory Oestreicher committed
86
87
  QPointer<UrlConfigurationDialog> dlg = new UrlConfigurationDialog( this );
  const int result = dlg->exec();
88

89
  if ( result == QDialog::Accepted && !dlg.isNull() ) {
90
    Settings::UrlConfiguration *urlConfig = new Settings::UrlConfiguration();
Tobias Koenig's avatar
Tobias Koenig committed
91

92
    urlConfig->mUrl = dlg->remoteUrl();
Grgory Oestreicher's avatar
Grgory Oestreicher committed
93
94
    urlConfig->mUser = dlg->username();
    urlConfig->mProtocol = dlg->protocol();
95

96
97
    Settings::self()->newUrlConfiguration( urlConfig );

Grgory Oestreicher's avatar
Grgory Oestreicher committed
98
    const QString protocolName = DavUtils::protocolName( dlg->protocol() );
Tobias Koenig's avatar
Tobias Koenig committed
99

Grgory Oestreicher's avatar
Grgory Oestreicher committed
100
101
    addModelRow( protocolName, dlg->remoteUrl() );
    mAddedUrls << dlg->remoteUrl();
Tobias Koenig's avatar
Tobias Koenig committed
102
    checkUserInput();
103
  }
104

Laurent Montel's avatar
Laurent Montel committed
105
  delete dlg;
106
107
108
109
}

void ConfigDialog::onRemoveButtonClicked()
{
Tobias Koenig's avatar
Tobias Koenig committed
110
  const QModelIndexList indexes = mUi.configuredUrls->selectionModel()->selectedRows();
Tobias Koenig's avatar
Tobias Koenig committed
111
  if ( indexes.size() == 0 )
112
    return;
113

114
  // There can be only one (selected row)
Tobias Koenig's avatar
Tobias Koenig committed
115
  const QModelIndex index = mModel->index( indexes.at( 0 ).row(), 1 );
116

Tobias Koenig's avatar
Tobias Koenig committed
117
118
119
120
  mRemovedUrls << index.data().toString();
  mModel->removeRow( index.row() );

  checkUserInput();
121
122
123
124
}

void ConfigDialog::onEditButtonClicked()
{
Tobias Koenig's avatar
Tobias Koenig committed
125
  const QModelIndexList indexes = mUi.configuredUrls->selectionModel()->selectedRows();
Tobias Koenig's avatar
Tobias Koenig committed
126
  if ( indexes.size() == 0 )
127
    return;
128

129
  // There can be only one (selected row)
Tobias Koenig's avatar
Tobias Koenig committed
130
131
  const QModelIndex index = mModel->index( indexes.at( 0 ).row(), 1 );
  const QString url = index.data().toString();
132

133
  Settings::UrlConfiguration *urlConfig = Settings::self()->urlConfiguration( url );
Tobias Koenig's avatar
Tobias Koenig committed
134
  if ( !urlConfig )
135
    return;
136

Grgory Oestreicher's avatar
Grgory Oestreicher committed
137
138
139
140
  QPointer<UrlConfigurationDialog> dlg = new UrlConfigurationDialog( this );
  dlg->setRemoteUrl( urlConfig->mUrl );
  dlg->setProtocol( DavUtils::Protocol( urlConfig->mProtocol ) );
  dlg->setUsername( urlConfig->mUser );
141

Grgory Oestreicher's avatar
Grgory Oestreicher committed
142
  const int result = dlg->exec();
143

144
  if ( result == QDialog::Accepted && !dlg.isNull() ) {
145
146
147
148
149
    Settings::UrlConfiguration *urlConfigAccepted = new Settings::UrlConfiguration();
    urlConfigAccepted->mUrl = dlg->remoteUrl();
    urlConfigAccepted->mUser = dlg->username();
    urlConfigAccepted->mProtocol = dlg->protocol();
    Settings::self()->newUrlConfiguration( urlConfigAccepted );
150

151
    QStandardItem *item = mModel->item( index.row(), 0 ); // Protocol
Grgory Oestreicher's avatar
Grgory Oestreicher committed
152
    item->setData( QVariant::fromValue( DavUtils::protocolName( dlg->protocol() ) ), Qt::DisplayRole );
153

154
    item = mModel->item( index.row(), 1 ); // URL
Grgory Oestreicher's avatar
Grgory Oestreicher committed
155
    item->setData( QVariant::fromValue( dlg->remoteUrl() ), Qt::DisplayRole );
156
  }
Laurent Montel's avatar
Laurent Montel committed
157
  delete dlg;
158
159
}

160
161
void ConfigDialog::onOkClicked()
{
Tobias Koenig's avatar
Tobias Koenig committed
162
  foreach ( const QString &url, mRemovedUrls )
163
    Settings::self()->removeUrlConfiguration( url );
164
165

  mManager->updateSettings();
166
167
168
169
}

void ConfigDialog::onCancelClicked()
{
170
  mRemovedUrls.clear();
171

Tobias Koenig's avatar
Tobias Koenig committed
172
  foreach ( const QString &url, mAddedUrls )
173
    Settings::self()->removeUrlConfiguration( url );
174
175
}

176
void ConfigDialog::checkConfiguredUrlsButtonsState()
177
{
Tobias Koenig's avatar
Tobias Koenig committed
178
179
  const bool enabled = mUi.configuredUrls->selectionModel()->hasSelection();

180
181
182
183
184
185
186
187
188
189
190
191
  mUi.removeButton->setEnabled( enabled );
  mUi.editButton->setEnabled( enabled );
}

void ConfigDialog::addModelRow( const QString &protocol, const QString &url )
{
  QStandardItem *rootItem = mModel->invisibleRootItem();
  QList<QStandardItem*> items;

  QStandardItem *protocolStandardItem = new QStandardItem( protocol );
  protocolStandardItem->setEditable( false );
  items << protocolStandardItem;
Tobias Koenig's avatar
Tobias Koenig committed
192

193
194
195
  QStandardItem *urlStandardItem = new QStandardItem( url );
  urlStandardItem->setEditable( false );
  items << urlStandardItem;
Tobias Koenig's avatar
Tobias Koenig committed
196

197
  rootItem->appendRow( items );
198
199
}

Tobias Koenig's avatar
Tobias Koenig committed
200
#include "configdialog.moc"