1 /* This file is part of the KDE project
2 Copyright (C) 2008 Eduardo Robles Elvira <edulix@gmail.com>
4 This program 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 2 of the License, or
7 (at your option) any later version.
9 This program 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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "konqsessiondlg.h"
21 #include "konqsettingsxt.h"
22 #include "konqviewmanager.h"
23 #include "konqsessionmanager.h"
24 #include "ui_konqsessiondlg_base.h"
25 #include "ui_konqnewsessiondlg_base.h"
27 #include <QtCore/QDir>
28 #include <QtGui/QFileSystemModel>
29 #include <QtGui/QListWidgetItem>
30 #include <QtCore/QFileInfo>
31 #include <QtCore/QDirIterator>
35 #include <kio/copyjob.h>
36 #include <kio/netaccess.h>
37 #include <kio/renamedialog.h>
38 #include <kfileitemdelegate.h>
39 #include <kdirlister.h>
40 #include <kdirmodel.h>
41 #include <kstandardguiitem.h>
42 #include <kio/global.h>
43 #include <kstandarddirs.h>
46 #include <kseparator.h>
47 #include <kmessagebox.h>
49 #include <klistwidget.h>
50 #include <kpushbutton.h>
52 class KonqSessionDlg::KonqSessionDlgPrivate
: public QWidget
,
53 public Ui::KonqSessionDlgBase
56 KonqSessionDlgPrivate( KonqViewManager
*manager
, QWidget
*parent
= 0 )
57 : QWidget( parent
) , m_pViewManager( manager
), m_pParent( parent
)
61 KonqViewManager
* const m_pViewManager
;
66 #define BTN_OPEN KDialog::User1
67 #define BTN_CLOSE KDialog::User2
69 KonqSessionDlg::KonqSessionDlg( KonqViewManager
*manager
, QWidget
*parent
)
71 , d( new KonqSessionDlgPrivate( manager
, this ) )
73 d
->layout()->setMargin( 0 );
76 setObjectName( "konq_session_dialog" );
78 setCaption( i18n( "Manage Sessions" ) );
79 setButtons( BTN_OPEN
| Close
);
80 setDefaultButton( BTN_CLOSE
);
81 showButtonSeparator( true );
83 setButtonGuiItem( BTN_OPEN
, KGuiItem( i18n( "&Open" ), "document-open" ) );
84 d
->m_pSaveCurrentButton
->setIcon(KIcon("document-save"));
85 d
->m_pRenameButton
->setIcon(KIcon("edit-rename"));
86 d
->m_pDeleteButton
->setIcon(KIcon("edit-delete"));
87 d
->m_pNewButton
->setIcon(KIcon("document-new"));
89 QString dir
= KStandardDirs::locateLocal("appdata", "sessions/");
91 d
->m_pModel
= new KDirModel(d
->m_pListView
);
92 d
->m_pModel
->sort(QDir::Name
);
93 d
->m_pModel
->dirLister()->setDirOnlyMode(true);
94 d
->m_pModel
->dirLister()->setAutoUpdate(true);
95 d
->m_pModel
->dirLister()->openUrl(dir
);
96 d
->m_pListView
->setModel(d
->m_pModel
);
98 d
->m_pListView
->setMinimumSize( d
->m_pListView
->sizeHint() );
100 connect( d
->m_pListView
->selectionModel(), SIGNAL( selectionChanged(
101 const QItemSelection
&, const QItemSelection
&) ), this, SLOT(
102 slotSelectionChanged() ) );
104 enableButton( BTN_OPEN
, d
->m_pListView
->currentIndex().isValid() );
105 slotSelectionChanged();
107 d
->m_pOpenTabsInsideCurrentWindow
->setChecked(
108 KonqSettings::openTabsInsideCurrentWindow());
110 connect( this,SIGNAL(user1Clicked()),SLOT(slotOpen()));
111 connect( d
->m_pNewButton
, SIGNAL(clicked()),SLOT(slotNew()));
112 connect( d
->m_pSaveCurrentButton
, SIGNAL(clicked()),SLOT(slotSave()));
113 connect( d
->m_pRenameButton
, SIGNAL(clicked()),SLOT(slotRename()));
114 connect( d
->m_pDeleteButton
, SIGNAL(clicked()),SLOT(slotDelete()));
116 resize( sizeHint() );
119 KonqSessionDlg::~KonqSessionDlg()
121 KonqSettings::setOpenTabsInsideCurrentWindow(
122 d
->m_pOpenTabsInsideCurrentWindow
->isChecked());
125 void KonqSessionDlg::slotOpen()
127 if(!d
->m_pListView
->currentIndex().isValid())
130 KonqSessionManager::self()->restoreSessions(d
->m_pModel
->itemForIndex(
131 d
->m_pListView
->currentIndex()).url().path(),
132 d
->m_pOpenTabsInsideCurrentWindow
->isChecked(),
133 reinterpret_cast<KonqMainWindow
*>(parent()));
137 void KonqSessionDlg::slotSave()
139 if(!d
->m_pListView
->currentIndex().isValid())
143 d
->m_pModel
->itemForIndex(d
->m_pListView
->currentIndex()).url().path());
144 QString dirpath
= "sessions/" + fileInfo
.fileName();
147 KonqSessionManager::self()->saveCurrentSessions(dirpath
);
150 void KonqSessionDlg::slotNew()
152 KonqNewSessionDlg
newDialog(this);
156 void KonqSessionDlg::slotDelete()
158 if(!d
->m_pListView
->currentIndex().isValid())
161 QDir
dir(d
->m_pModel
->itemForIndex(d
->m_pListView
->currentIndex()).url().path());
164 KIO::NetAccess::del(d
->m_pModel
->itemForIndex(
165 d
->m_pListView
->currentIndex()).url(), d
->m_pParent
);
169 void KonqSessionDlg::slotRename(KUrl dirpathTo
)
171 if ( !d
->m_pListView
->currentIndex().isValid() )
174 KUrl dirpathFrom
= d
->m_pModel
->itemForIndex(
175 d
->m_pListView
->currentIndex()).url();
177 dirpathTo
= (dirpathTo
== KUrl()) ? dirpathFrom
: dirpathTo
;
179 KIO::RenameDialog
dlg(this, i18n("Rename session"), dirpathFrom
,
180 dirpathTo
, KIO::RenameDialog_Mode(0));
182 if(dlg
.exec() == KIO::R_RENAME
)
184 dirpathTo
= dlg
.newDestUrl();
185 QDir
dir(dirpathTo
.path());
187 slotRename(dirpathTo
);
189 QDir
dir(KStandardDirs::locateLocal("appdata", "sessions/"));
190 dir
.rename(dirpathFrom
.fileName(), dlg
.newDestUrl().fileName());
195 void KonqSessionDlg::slotSelectionChanged()
197 bool enable
= !d
->m_pListView
->selectionModel()->selectedIndexes().isEmpty();
198 d
->m_pSaveCurrentButton
->setEnabled( enable
);
199 d
->m_pRenameButton
->setEnabled( enable
);
200 d
->m_pDeleteButton
->setEnabled( enable
);
201 enableButton( BTN_OPEN
, enable
);
207 class KonqNewSessionDlg::KonqNewSessionDlgPrivate
: public QWidget
,
208 public Ui::KonqNewSessionDlgBase
211 KonqNewSessionDlgPrivate( QWidget
*parent
= 0 )
212 : QWidget( parent
), m_pParent( parent
)
219 KonqNewSessionDlg::KonqNewSessionDlg( QWidget
*parent
, QString sessionName
)
221 , d( new KonqNewSessionDlgPrivate( this ) )
223 d
->layout()->setMargin( 0 );
226 setObjectName( "konq_new_session_dialog" );
228 setCaption( i18n( "Save Session" ) );
229 setButtons( Ok
| Cancel
);
230 setDefaultButton( Ok
);
231 enableButton( Ok
, false );
232 showButtonSeparator( true );
234 if(!sessionName
.isEmpty())
235 d
->m_pSessionName
->setText(sessionName
);
237 connect(this, SIGNAL(okClicked()), SLOT(slotAddSession()));
238 connect(d
->m_pSessionName
, SIGNAL(textChanged(const QString
&)), this,
239 SLOT(slotTextChanged(const QString
&)));
241 resize( sizeHint() );
244 void KonqNewSessionDlg::slotAddSession()
246 QString dirpath
= KStandardDirs::locateLocal("appdata", "sessions/" +
247 d
->m_pSessionName
->text());
252 if(KMessageBox::questionYesNo(this,
253 i18n("A session with the name '%1' already exists, do you want to overwrite it?", d
->m_pSessionName
->text()),
254 i18n("Session exists. Overwrite?")) == KMessageBox::Yes
)
256 KIO::NetAccess::del(KUrl(dirpath
), d
->m_pParent
);
258 KonqNewSessionDlg
newDialog(d
->m_pParent
,
259 d
->m_pSessionName
->text());
263 KonqSessionManager::self()->saveCurrentSessions(dirpath
);
266 void KonqNewSessionDlg::slotTextChanged(const QString
& text
)
268 enableButton( Ok
, !text
.isEmpty() );
271 KonqNewSessionDlg::~KonqNewSessionDlg()
275 #include "konqsessiondlg.moc"