1/*
2 * kdfconfig.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 "kdfconfig.h"
28
29#include <QtGui/QCheckBox>
30#include <QtGui/QLabel>
31#include <QtGui/QLCDNumber>
32#include <QtGui/QVBoxLayout>
33#include <QtGui/QCloseEvent>
34#include <QtGui/QTreeWidget>
35#include <QtGui/QTreeWidgetItem>
36#include <QtGui/QSizePolicy>
37
38#include <kapplication.h>
39#include <kconfig.h>
40#include <kdialog.h>
41#include <klocale.h>
42#include <kiconloader.h>
43#include <kglobal.h>
44#include <klineedit.h>
45
46#ifndef GUI_DEFINED
47static bool GUI;
48#define GUI_DEFINED
49#endif
50
51
52KDFConfigWidget::KDFConfigWidget(QWidget *parent, bool init)
53 : QWidget( parent)
54{
55
56 m_columnList.append( Column( QLatin1String( "Icon" ), i18nc("TODO", "Icon"), 20, KDFWidget::IconCol) );
57 m_columnList.append( Column( QLatin1String( "Device" ), i18nc("Device of the storage", "Device"), 100, KDFWidget::DeviceCol) );
58 m_columnList.append( Column( QLatin1String( "Type" ), i18nc("Filesystem on storage", "Type"), 100 , KDFWidget::TypeCol) );
59 m_columnList.append( Column( QLatin1String( "Size" ), i18nc("Total size of the storage", "Size"), 100, KDFWidget::SizeCol) );
60 m_columnList.append( Column( QLatin1String( "MountPoint" ), i18nc("Mount point of the storage", "Mount Point"), 100, KDFWidget::MountPointCol) );
61 m_columnList.append( Column( QLatin1String( "Free" ), i18nc("Free space in storage", "Free"), 100, KDFWidget::FreeCol) );
62 m_columnList.append( Column( QLatin1String( "Full%" ), i18nc("Used storage space in %", "Full %"), 50, KDFWidget::FullCol ));
63 m_columnList.append( Column( QLatin1String( "UsageBar" ), i18nc("Usage graphical bar", "Usage"), 100, KDFWidget::UsageBarCol) );
64
65 GUI = !init;
66 if( GUI )
67 {
68 setupUi(this);
69
70 QStringList headerLabels;
71 Q_FOREACH( const Column &c, m_columnList)
72 {
73 headerLabels << c.columnName;
74 }
75 m_listWidget->setHeaderLabels(headerLabels);
76
77 connect( m_listWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
78 this, SLOT(toggleListText(QTreeWidgetItem*,int)) );
79 connect( m_listWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
80 this, SLOT(slotChanged()) );
81
82 iconVisible = MainBarIcon( QLatin1String( "dialog-ok-apply" ) );
83 iconHidden = MainBarIcon( QLatin1String( "dialog-cancel" ) );
84
85 QTreeWidgetItem * item = new QTreeWidgetItem( m_listWidget );
86 for( int i=0; i < m_columnList.size(); i++ )
87 {
88 item->setText( i, i18nc("Visible items on device information columns (enable|disable)", "visible") );
89 item->setIcon( i, iconVisible );
90 item->setData( i, Qt::UserRole, QVariant( true ) );
91 }
92 m_listWidget->setCurrentItem( item );
93
94 connect(m_updateSpinBox, SIGNAL(valueChanged(int)), this, SLOT(slotChanged()) );
95
96 connect(mFileManagerEdit,SIGNAL(textChanged(QString)),this,SLOT(slotChanged()));
97
98 connect(mOpenMountCheck,SIGNAL(toggled(bool)),this,SLOT(slotChanged()));
99
100 connect(mPopupFullCheck,SIGNAL(toggled(bool)),this,SLOT(slotChanged()));
101 }
102
103 loadSettings();
104 if( init )
105 applySettings();
106
107}
108
109
110KDFConfigWidget::~KDFConfigWidget()
111{
112 delete m_listWidget;
113}
114
115
116void KDFConfigWidget::closeEvent(QCloseEvent *)
117{
118 applySettings();
119 kapp->quit();
120}
121
122
123void KDFConfigWidget::applySettings( void )
124{
125 KConfigGroup config(KGlobal::config(), "KDFConfig");
126
127 if( GUI )
128 {
129 //config.writeEntry( "Width", width() );
130 //config.writeEntry( "Height", height() );
131
132 mStd.setFileManager( mFileManagerEdit->text() );
133 mStd.setUpdateFrequency( m_updateSpinBox->value() );
134 mStd.setPopupIfFull( mPopupFullCheck->isChecked() );
135 mStd.setOpenFileManager( mOpenMountCheck->isChecked() );
136 mStd.writeConfiguration();
137
138 QTreeWidgetItem * item = m_listWidget->topLevelItem(0);
139 for( int i=0; i < m_columnList.size(); i++ )
140 {
141 bool visible = item->data( i, Qt::UserRole ).toBool();
142 config.writeEntry( m_columnList.at(i).name , visible );
143 }
144
145 }
146 else
147 {
148 mStd.writeDefaultFileManager();
149 }
150 config.sync();
151}
152
153
154void KDFConfigWidget::loadSettings( void )
155{
156 KConfigGroup config(KGlobal::config(), "KDFConfig");
157
158 if( GUI )
159 {
160 mStd.updateConfiguration();
161 m_updateSpinBox->setValue( mStd.updateFrequency() );
162 mPopupFullCheck->setChecked( mStd.popupIfFull() );
163 mOpenMountCheck->setChecked( mStd.openFileManager() );
164 mFileManagerEdit->setText( mStd.fileManager() );
165
166 QTreeWidgetItem * item = m_listWidget->topLevelItem(0);
167 for( int i=0; i < m_columnList.size(); i++ )
168 {
169 bool visible = config.readEntry( m_columnList.at(i).name, true );
170
171 item->setText( i, visible ? i18nc("Are items on device information columns visible?", "visible") :
172 i18nc("Are items on device information columns hidden?", "hidden") );
173 item->setIcon( i, visible ? iconVisible : iconHidden );
174 item->setData( i, Qt::UserRole, QVariant( visible ) );
175 }
176 }
177
178}
179
180void KDFConfigWidget::defaultsBtnClicked( void )
181{
182 mStd.setDefault();
183 m_updateSpinBox->setValue( mStd.updateFrequency() );
184 mPopupFullCheck->setChecked( mStd.popupIfFull() );
185 mOpenMountCheck->setChecked( mStd.openFileManager() );
186 mFileManagerEdit->setText( mStd.fileManager() );
187
188 QTreeWidgetItem * item = m_listWidget->topLevelItem(0);
189 for( int i=0; i < m_columnList.size(); i++ )
190 {
191 item->setText( i, i18nc("Visible items on device information columns (enable|disable)", "visible") );
192 item->setIcon( i, iconVisible );
193 item->setData( i, Qt::UserRole, QVariant( true ) );
194 }
195 m_listWidget->setCurrentItem( item );
196}
197
198
199void KDFConfigWidget::toggleListText( QTreeWidgetItem *item, int column )
200{
201 if (!item)
202 return;
203 QString text = item->text( column );
204
205 bool visible = item->data( column, Qt::UserRole).toBool();
206 item->setData( column, Qt::UserRole, !visible );
207
208 item->setText(column, visible ? i18nc("Device information item is hidden", "hidden"):
209 i18nc("Device information item is visible", "visible") );
210 item->setIcon( column, visible ? iconHidden : iconVisible );
211}
212
213void KDFConfigWidget::slotChanged()
214{
215 emit configChanged();
216}
217
218#include "kdfconfig.moc"
219
220