mainwindow.cpp 6.95 KB
Newer Older
1
2
3
/*
 * ark -- archiver for the KDE project
 *
4
 * Copyright (C) 2002-2003: Georg Robbers <Georg.Robbers@urz.uni-hd.de>
5
6
7
 * Copyright (C) 2003: Helio Chissini de Castro <helio@conectiva.com>
 * Copyright (C) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
 * Copyright (C) 2008 Harald Hvaal <haraldhv@stud.ntnu.no>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * 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.
 *
 * 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.
 *
 * 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.
 *
 */
Henrique Pinto's avatar
Henrique Pinto committed
24
#include "mainwindow.h"
25
#include "part/interface.h"
Henrique Pinto's avatar
Henrique Pinto committed
26

27
28
#include <KPluginLoader>
#include <KPluginFactory>
Henrique Pinto's avatar
Henrique Pinto committed
29
30
31
32
33
34
#include <KMessageBox>
#include <KApplication>
#include <KLocale>
#include <KActionCollection>
#include <KStandardAction>
#include <KFileDialog>
35
36
#include <KRecentFilesAction>
#include <KGlobal>
Andreas Pakulat's avatar
Andreas Pakulat committed
37
#include <KDebug>
38
39
#include <KEditToolBar>
#include <KShortcutsDialog>
Henrique Pinto's avatar
Henrique Pinto committed
40

41
42
#include <QDragEnterEvent>
#include <QDragMoveEvent>
43
#include <QPointer>
44

45
46
47
48
49
50
51
static bool isValidArchiveDrag(const QMimeData *data)
{
	return
	data->hasUrls() &&
	data->urls().count() == 1;
}

Henrique Pinto's avatar
Henrique Pinto committed
52
53
54
55
56
57
MainWindow::MainWindow( QWidget * )
	: KParts::MainWindow( )
{
	setXMLFile( "arkui.rc" );

	setupActions();
58
	statusBar();
Henrique Pinto's avatar
Henrique Pinto committed
59
60
61
62
63
64

	if ( !initialGeometrySet() )
	{
		resize( 640, 480 );
	}
	setAutoSaveSettings( "MainWindow" );
65
66

	setAcceptDrops(true);
Henrique Pinto's avatar
Henrique Pinto committed
67
68
69
70
}

MainWindow::~MainWindow()
{
71
72
73
74
	if ( m_recentFilesAction )
	{
		m_recentFilesAction->saveEntries( KGlobal::config()->group( "Recent Files" ) );
	}
75
76
	delete m_part;
	m_part = 0;
Henrique Pinto's avatar
Henrique Pinto committed
77
78
}

79
80
81
void MainWindow::dragEnterEvent ( QDragEnterEvent * event )
{
	kDebug(1601) << event;
82
83
84
85

	Interface *iface = qobject_cast<Interface*>( m_part );
	if (iface->isBusy()) return;

86
87
	if (event->source() == NULL &&
			isValidArchiveDrag(event->mimeData()))
88
89
90
91
92
93
94
         event->acceptProposedAction();
	return;
}

void MainWindow::dropEvent ( QDropEvent * event )
{
	kDebug(1601) << event;
95
96
97

	Interface *iface = qobject_cast<Interface*>( m_part );
	if (iface->isBusy()) return;
98
	
99
100
	if (event->source() == NULL &&
			isValidArchiveDrag(event->mimeData()))
Harald Hvaal's avatar
Harald Hvaal committed
101
         event->acceptProposedAction();
102

103
104
	//TODO: if this call provokes a message box the drag will still be going
	//while the box is onscreen. looks buggy, do something about it
105
106
107
108
109
110
111
	openUrl(event->mimeData()->urls().at(0));
}

void MainWindow::dragMoveEvent ( QDragMoveEvent * event )
{
	kDebug(1601) << event;

112
113
114
	Interface *iface = qobject_cast<Interface*>( m_part );
	if (iface->isBusy()) return;

115
116
	if (event->source() == NULL &&
			isValidArchiveDrag(event->mimeData()))
Harald Hvaal's avatar
Harald Hvaal committed
117
         event->acceptProposedAction();
118
119
}

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
bool MainWindow::loadPart()
{
	KPluginFactory *factory = KPluginLoader("libarkpart").factory();
	if(factory) {
		m_part = static_cast<KParts::ReadWritePart*>( factory->create<KParts::ReadWritePart>(this) );
	}
	if ( !factory || !m_part )
	{
		KMessageBox::error( this, i18n( "Unable to find Ark's KPart component, please check your installation." ) );
		return false;
	}

	m_part->setObjectName( "ArkPart" );
	setCentralWidget( m_part->widget() );
	createGUI( m_part );

	connect( m_part, SIGNAL( busy() ), this, SLOT( updateActions() ) );
	connect( m_part, SIGNAL( ready() ), this, SLOT( updateActions() ) );

	return true;	
}

Henrique Pinto's avatar
Henrique Pinto committed
142
143
void MainWindow::setupActions()
{
144
145
	m_newAction = KStandardAction::openNew( this, SLOT( newArchive() ), actionCollection() );
	m_openAction = KStandardAction::open( this, SLOT( openArchive() ), actionCollection() );
Henrique Pinto's avatar
Henrique Pinto committed
146
	KStandardAction::quit( this, SLOT( quit() ), actionCollection() );
147
148
149
150

	m_recentFilesAction = KStandardAction::openRecent( this, SLOT( openUrl( const KUrl& ) ), actionCollection() );
	m_recentFilesAction->setToolBarMode( KRecentFilesAction::MenuMode );
	m_recentFilesAction->setToolButtonPopupMode( QToolButton::DelayedPopup );
Henrique Pinto's avatar
Henrique Pinto committed
151
	m_recentFilesAction->setIconText( i18nc( "action, to open an archive", "Open" ) );
152
153
154
155
156
	m_recentFilesAction->setStatusTip( i18n( "Click to open an archive, click and hold to open a recently-opened archive" ) );
	m_recentFilesAction->setToolTip( i18n( "Open an archive" ) );
	m_recentFilesAction->loadEntries( KGlobal::config()->group( "Recent Files" ) );
	connect( m_recentFilesAction, SIGNAL( triggered() ),
	         this, SLOT( openArchive() ) );
157
158
159
160
161
162
163

	createStandardStatusBarAction();

	KStandardAction::configureToolbars( this, SLOT( editToolbars() ), actionCollection() );
	KStandardAction::keyBindings( this, SLOT( editKeyBindings() ), actionCollection() );
}

164
165
166
167
168
169
170
171
void MainWindow::updateActions()
{
	Interface *iface = qobject_cast<Interface*>( m_part );
	m_newAction->setEnabled( !iface->isBusy() );
	m_openAction->setEnabled( !iface->isBusy() );
	m_recentFilesAction->setEnabled( !iface->isBusy() );
}

172
173
174
175
176
177
178
179
180
181
182
void MainWindow::editKeyBindings()
{
	KShortcutsDialog dlg( KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this );
	dlg.addCollection( actionCollection() );
	dlg.addCollection( m_part->actionCollection() );

	dlg.configure();
}

void MainWindow::editToolbars()
{
183
184
185
186
187
	saveMainWindowSettings( KGlobal::config()->group( QLatin1String( "MainWindow" ) ) );

	QPointer<KEditToolBar> dlg = new KEditToolBar( factory(), this );
	dlg->exec();

188
	createGUI( m_part );
189
190

	applyMainWindowSettings( KGlobal::config()->group( QLatin1String( "MainWindow" ) ) );
Henrique Pinto's avatar
Henrique Pinto committed
191
192
193
194
}

void MainWindow::openArchive()
{
195
196
197
198
199
	Interface *iface = qobject_cast<Interface*>( m_part );
	Q_ASSERT( iface );
	KUrl url = KFileDialog::getOpenUrl( KUrl( "kfiledialog:///ArkOpenDir"),
	                                    iface->supportedMimeTypes().join( " " ),
	                                    this );
200
201
	openUrl( url );
}
Henrique Pinto's avatar
Henrique Pinto committed
202

203
204
void MainWindow::openUrl( const KUrl& url )
{
Henrique Pinto's avatar
Henrique Pinto committed
205
206
	if ( !url.isEmpty() )
	{
207
208
		m_part->setArguments(m_openArgs);

209
210
211
212
213
214
215
216
		if ( m_part->openUrl( url ) )
		{
			m_recentFilesAction->addUrl( url );
		}
		else
		{
			m_recentFilesAction->removeUrl( url );
		}
Henrique Pinto's avatar
Henrique Pinto committed
217
218
219
	}
}

220
221
222
223
224
225
226
227
228
229
230
231
void MainWindow::setShowExtractDialog(bool option)
{
	if (option)
	{
		m_openArgs.metaData()["showExtractDialog"] = "true";
	}
	else
	{
		m_openArgs.metaData().remove("showExtractDialog");
	}
}

Henrique Pinto's avatar
Henrique Pinto committed
232
233
void MainWindow::quit()
{
234
235
236
237
238
239
240
241
242
	close();
}

void MainWindow::newArchive()
{
	Interface *iface = qobject_cast<Interface*>( m_part );
	Q_ASSERT( iface );
	QStringList mimeTypes = iface->supportedWriteMimeTypes();

Thiago Macieira's avatar
Thiago Macieira committed
243
	kDebug( 1601 ) << "Supported mimetypes are" << mimeTypes.join( " " );
244
245
246

	QString saveFile = KFileDialog::getSaveFileName( KUrl( "kfiledialog:///ArkNewDir" ),
	                                                 mimeTypes.join( " " ) );
247
248
249

	m_openArgs.metaData()["createNewArchive"] = "true";

250
	openUrl( KUrl( saveFile ) );
251
252
253

	m_openArgs.metaData().remove("showExtractDialog");
	m_openArgs.metaData().remove("createNewArchive");
Henrique Pinto's avatar
Henrique Pinto committed
254
}