1/*
2 * mntconfig.cpp
3 *
4 * Copyright (c) 1999 Michael Kropfberger <michael.kropfberger@gmx.net>
5 * 2009 Dario Andres Rodriguez <andresbajotierra@gmail.com>
6
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22//
23// 1999-11-29 Espen Sand
24// Converted to QLayout and QListView + cleanups
25//
26
27#include "mntconfig.h"
28
29#include <QtGui/QGroupBox>
30#include <QtGui/QLabel>
31#include <QtGui/QPixmap>
32#include <QtGui/QHBoxLayout>
33#include <QtGui/QFormLayout>
34#include <QtGui/QCloseEvent>
35#include <QtGui/QTreeWidget>
36#include <QtGui/QTreeWidgetItem>
37
38#include <kfiledialog.h>
39#include <kicondialog.h>
40#include <kmessagebox.h>
41#include <kglobal.h>
42#include <kdebug.h>
43#include <klocale.h>
44#include <klineedit.h>
45#include <kcmodule.h>
46#include <kconfig.h>
47#include <kiconloader.h>
48
49#ifndef KDE_USE_FINAL
50static bool GUI;
51#endif
52
53MntConfigWidget::MntConfigWidget(QWidget *parent, bool init)
54 : QWidget(parent)
55{
56 mInitializing = false;
57
58 GUI = !init;
59 if (GUI)
60 {
61 setupUi(this);
62
63 //tabList fillup waits until disklist.readDF() is done...
64 mDiskList.readFSTAB();
65 mDiskList.readDF();
66 mInitializing = true;
67 connect( &mDiskList, SIGNAL(readDFDone()), this, SLOT(readDFDone()));
68
69 connect ( m_listWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)) , this, SLOT(clicked(QTreeWidgetItem*,int)) );
70 m_listWidget->setHeaderLabels( QStringList() << QLatin1String( "" ) << i18n("Device")
71 << i18n("Mount Point") << i18n("Mount Command") << i18n("Unmount Command") );
72 m_listWidget->setColumnWidth( 0, 20 );
73
74 QString text = QString::fromLatin1("%1: %2 %3: %4").
75 arg(i18n("Device")).
76 arg(i18nc("No device is selected", "None")).
77 arg(i18n("Mount Point")).
78 arg(i18nc("No mount point is selected", "None"));
79
80 mGroupBox->setEnabled( false );
81 mGroupBox->setTitle(text);
82
83 connect( mIconLineEdit, SIGNAL(textEdited(QString)),
84 this,SLOT(iconChanged(QString)));
85 connect( mIconLineEdit, SIGNAL(textEdited(QString)),
86 this,SLOT(slotChanged()));
87
88 mIconButton->setIconType(KIconLoader::Small, KIconLoader::Device);
89 mIconButton->setFixedHeight( mIconButton->sizeHint().height() );
90
91 connect( mIconButton, SIGNAL(iconChanged(QString)), this, SLOT(iconChangedButton(QString)));
92 connect( mIconButton, SIGNAL(iconChanged(QString)), this, SLOT(slotChanged()));
93
94 connect( mDefaultIconButton, SIGNAL(clicked()), this, SLOT(iconDefault()) );
95 connect( mDefaultIconButton, SIGNAL(clicked()), this, SLOT(slotChanged()) );
96
97 connect( mMountLineEdit,SIGNAL(textChanged(QString)),
98 this,SLOT(mntCmdChanged(QString)));
99 connect( mMountLineEdit, SIGNAL(textChanged(QString)),
100 this,SLOT(slotChanged()));
101
102 connect( mMountButton, SIGNAL(clicked()), this, SLOT(selectMntFile()) );
103
104 connect( mUmountLineEdit, SIGNAL(textChanged(QString)),
105 this,SLOT(umntCmdChanged(QString)));
106 connect( mUmountLineEdit, SIGNAL(textChanged(QString)),
107 this,SLOT(slotChanged()));
108
109 connect( mUmountButton,SIGNAL(clicked()),this,SLOT(selectUmntFile()));
110 }
111
112 loadSettings();
113 if(init)
114 {
115 applySettings();
116 }
117}
118
119
120MntConfigWidget::~MntConfigWidget( void )
121{
122 delete m_listWidget;
123}
124
125
126void MntConfigWidget::readDFDone( void )
127{
128 mInitializing = false;
129 m_listWidget->clear();
130
131 QTreeWidgetItem *item = 0;
132
133 DisksConstIterator itr = mDiskList.disksConstIteratorBegin();
134 DisksConstIterator end = mDiskList.disksConstIteratorEnd();
135 for (; itr != end; ++itr)
136 {
137 DiskEntry * disk = *itr;
138 item = new QTreeWidgetItem( m_listWidget, QStringList() << QString() << disk->deviceName()
139 << disk->mountPoint() << disk->mountCommand() << disk->umountCommand() );
140 item->setIcon( IconCol, SmallIcon( disk->iconName() ) );
141 }
142
143 /*
144 //Adjust dialog size (?)
145 m_listWidget->resizeColumnToContents( 2 );
146 m_listWidget->resize(m_listWidget->sizeHint());
147 m_listWidget->adjustSize();
148 */
149
150 loadSettings();
151 applySettings();
152}
153
154
155void MntConfigWidget::applySettings( void )
156{
157 mDiskList.applySettings();
158
159 KConfigGroup config(KGlobal::config(), "MntConfig");
160 if( GUI )
161 {
162 config.writeEntry("Width", width() );
163 config.writeEntry("Height", height() );
164 }
165 config.sync();
166}
167
168
169void MntConfigWidget::loadSettings( void )
170{
171 KConfigGroup config = KGlobal::config()->group("MntConfig");
172 if( mInitializing == false && GUI )
173 {
174 if( isTopLevel() )
175 {
176 int w = config.readEntry("Width",this->width() );
177 int h = config.readEntry("Height",this->height() );
178 resize(w,h);
179 }
180
181 QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
182 if( list.size() == 1 )
183 clicked( list.at(0), 0 );
184 }
185}
186
187
188void MntConfigWidget::clicked( QTreeWidgetItem * item , int col )
189{
190 Q_UNUSED(col);
191
192 QTreeWidgetItem * header = m_listWidget->headerItem();
193
194 mGroupBox->setEnabled( true );
195 mGroupBox->setTitle( QString::fromLatin1("%1: %2 %3: %4").
196 arg(header->text( DeviceCol )).
197 arg(item->text( DeviceCol )).
198 arg(header->text( MountPointCol )).
199 arg(item->text( MountPointCol )) );
200
201
202 const QIcon icon = item->icon( IconCol );
203 if( !icon.isNull() )
204 mIconButton->setIcon( icon );
205
206 DiskEntry * disk = selectedDisk( item );
207 if (!disk)
208 return;
209
210 mIconLineEdit->setText( disk->iconName() );
211
212 mMountLineEdit->setText( item->text( MountCommandCol ) );
213 mUmountLineEdit->setText( item->text( UmountCommandCol ) );
214}
215
216
217void MntConfigWidget::iconChangedButton(const QString &iconName)
218{
219 iconChanged(iconName);
220}
221
222void MntConfigWidget::iconChanged(const QString &iconName)
223{
224 QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
225 QTreeWidgetItem * item = list.at(0);
226
227 DiskEntry * disk = selectedDisk( item );
228 if ( !disk )
229 return;
230
231 disk->setIconName(iconName);
232 mIconLineEdit->setText(iconName);
233
234 QPixmap icon = SmallIcon( iconName );
235 item->setIcon( IconCol, icon );
236 mIconButton->setIcon( icon );
237
238 slotChanged();
239}
240
241void MntConfigWidget::iconDefault()
242{
243 QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
244 QTreeWidgetItem * item = list.at(0);
245
246 DiskEntry * disk = selectedDisk( item );
247 if ( !disk )
248 return;
249
250 iconChanged(disk->guessIconName());
251}
252
253void MntConfigWidget::selectMntFile()
254{
255 KUrl url = KFileDialog::getOpenUrl( KUrl(),QLatin1String( "*" ), this );
256
257 if( url.isEmpty() )
258 return;
259
260 if( !url.isLocalFile() )
261 {
262 KMessageBox::sorry( 0L, i18n( "Only local files supported." ) );
263 return;
264 }
265
266 mMountLineEdit->setText( url.path() );
267}
268
269void MntConfigWidget::selectUmntFile()
270{
271 KUrl url = KFileDialog::getOpenUrl( KUrl(), QLatin1String( "*" ), this );
272
273 if( url.isEmpty() )
274 return;
275
276 if( !url.isLocalFile() )
277 {
278 KMessageBox::sorry( 0L, i18n( "Only local files are currently supported." ) );
279 return;
280 }
281
282 mUmountLineEdit->setText( url.path() );
283}
284
285void MntConfigWidget::mntCmdChanged( const QString &data )
286{
287
288 QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
289 QTreeWidgetItem * item = list.at(0);
290
291 DiskEntry * disk = selectedDisk( item );
292 if ( !disk )
293 return;
294
295 disk->setMountCommand( data );
296 item->setText( MountCommandCol , data );
297
298}
299
300
301void MntConfigWidget::umntCmdChanged( const QString &data )
302{
303 QList<QTreeWidgetItem*> list = m_listWidget->selectedItems();
304 QTreeWidgetItem * item = list.at(0);
305
306 DiskEntry * disk = selectedDisk( item );
307 if ( !disk )
308 return;
309
310 disk->setUmountCommand( data );
311 item->setText( UmountCommandCol , data );
312
313}
314
315DiskEntry * MntConfigWidget::selectedDisk( QTreeWidgetItem * item )
316{
317 if( item == 0 )
318 {
319 QList<QTreeWidgetItem*> selected = m_listWidget->selectedItems();
320 if ( selected.size() == 1 )
321 item = selected.at(0);
322 else
323 return 0;
324 }
325
326 DiskEntry * tmpDisk = new DiskEntry(item->text( DeviceCol ));
327 tmpDisk->setMountPoint(item->text( MountPointCol ));
328
329 int pos = mDiskList.find(tmpDisk);
330
331 delete tmpDisk;
332
333 return mDiskList.at(pos);
334}
335
336void MntConfigWidget::closeEvent(QCloseEvent *)
337{
338}
339
340void MntConfigWidget::slotChanged()
341{
342 emit configChanged();
343}
344
345#include "mntconfig.moc"
346
347