1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
/*
ark -- archiver for the KDE project
Copyright (C) 2002-2003: Georg Robbers <Georg.Robbers@urz.uni-hd.de>
Copyright (C) 2003: Helio Chissini de Castro <helio@conectiva.com>
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.
*/
// QT includes
#include <qlayout.h>
// KDE includes
#include <kdebug.h>
#include <klocale.h>
#include <kedittoolbar.h>
#include <kstatusbar.h>
#include <kfiledialog.h>
#include <kmessagebox.h>
#include <kpopupmenu.h>
#include <kparts/componentfactory.h>
#include <kparts/browserextension.h>
#include <kkeydialog.h>
#include <kcombobox.h>
#include <kio/netaccess.h>
#include <kaccel.h>
// ark includes
#include "arkapp.h"
#include "settings.h"
#include "archiveformatinfo.h"
#include "arkwidget.h"
MainWindow::MainWindow( QWidget * /*parent*/, const char *name )
: KParts::MainWindow(), progressDialog( 0 )
{
setXMLFile( "arkui.rc" );
m_part = KParts::ComponentFactory::createPartInstanceFromLibrary<KParts::ReadWritePart>( "libarkpart", this, name, this, "ArkPart");
if (m_part )
{
//Since most of the functionality is still in ArkWidget:
m_widget = static_cast< ArkWidget* >( m_part->widget() );
setStandardToolBarMenuEnabled( true );
setupActions();
connect( m_part->widget(), SIGNAL( request_file_quit() ), this, SLOT( file_quit() ) );
connect( KParts::BrowserExtension::childObject( m_part ), SIGNAL( openURLRequestDelayed
( const KURL &, const KParts::URLArgs & ) ),
m_part, SLOT( openURL( const KURL & ) ) );
m_widget->setArchivePopupEnabled( true );
connect( m_part->widget(), SIGNAL( signalArchivePopup( const QPoint & ) ), this,
SLOT( slotArchivePopup( const QPoint & ) ) );
connect( m_part, SIGNAL( removeRecentURL( const KURL & ) ), this,
SLOT( slotRemoveRecentURL( const KURL & ) ) );
connect( m_part, SIGNAL( addRecentURL( const KURL & ) ), this,
SLOT( slotAddRecentURL( const KURL & ) ) );
connect( m_part, SIGNAL( fixActionState( const bool & ) ), this,
SLOT( slotFixActionState( const bool & ) ) );
connect( m_widget, SIGNAL( disableAllActions() ), this,
SLOT( slotDisableActions() ) );
ArkApplication::getInstance()->addWindow();
connect( m_widget, SIGNAL( removeOpenArk( const KURL &) ), this,
SLOT( slotRemoveOpenArk( const KURL & ) ) );
connect( m_widget, SIGNAL( addOpenArk( const KURL & ) ), this,
SLOT( slotAddOpenArk( const KURL & ) ) );
setCentralWidget( m_part->widget() );
createGUI( m_part );
if ( !initialGeometrySet() )
{
resize( 640, 300 );
}
setAutoSaveSettings( "MainWindow" );
}
else
kdFatal( 1601 ) << "libark could not found. Aborting. " << endl;
}
MainWindow::~MainWindow()
{
ArkApplication::getInstance()->removeWindow();
delete m_part;
delete progressDialog;
progressDialog = 0;
}
void
MainWindow::setupActions()
{
newWindowAction = new KAction(i18n("New &Window"), "window_new", KShortcut(), this,
SLOT(file_newWindow()), actionCollection(), "new_window");
newArchAction = KStdAction::openNew(this, SLOT(file_new()), actionCollection());
openAction = KStdAction::open(this, SLOT(file_open()), actionCollection());
reloadAction = new KAction(i18n("Re&load"), "reload", KStdAccel::shortcut( KStdAccel::Reload ), this,
SLOT(file_reload()), actionCollection(), "reload_arch");
closeAction = KStdAction::close(this, SLOT(file_close()), actionCollection(), "file_close");
recent = KStdAction::openRecent(this, SLOT(openURL(const KURL&)), actionCollection());
recent->loadEntries(kapp->config());
createStandardStatusBarAction();
KStdAction::quit(this, SLOT(window_close()), actionCollection());
KStdAction::configureToolbars(this, SLOT(editToolbars()), actionCollection());
KStdAction::keyBindings(this, SLOT( slotConfigureKeyBindings()), actionCollection());
openAction->setEnabled( true );
recent->setEnabled( true );
closeAction->setEnabled( false );
reloadAction->setEnabled( false );
}
void
MainWindow::slotDisableActions()
{
openAction->setEnabled(false);
newArchAction->setEnabled(false);
closeAction->setEnabled(false);
reloadAction->setEnabled(false);
}
void
MainWindow::slotFixActionState( const bool & bHaveFiles )
{
openAction->setEnabled(true);
newArchAction->setEnabled(true);
closeAction->setEnabled(bHaveFiles);
reloadAction->setEnabled(bHaveFiles);
}
void
MainWindow::file_newWindow()
{
MainWindow *kw = new MainWindow;
kw->resize( 640, 300 );
kw->show();
}
void
MainWindow::file_new()
{
m_widget->file_new();
}
void
MainWindow::file_reload()
{
KURL url( m_part->url() );
file_close();
m_part->openURL( url );
}
void
MainWindow::editToolbars()
{
saveMainWindowSettings( KGlobal::config(), QString::fromLatin1("MainWindow") );
KEditToolbar dlg( factory(), this );
connect(&dlg, SIGNAL( newToolbarConfig() ), this, SLOT( slotNewToolbarConfig() ));
dlg.exec();
}
void
MainWindow::slotNewToolbarConfig()
{
createGUI( m_part );
applyMainWindowSettings( KGlobal::config(), QString::fromLatin1("MainWindow") );
}
void
MainWindow::slotConfigureKeyBindings()
{
KKeyDialog dlg( true, this );
dlg.insert( actionCollection() );
dlg.insert( m_part->actionCollection() );
dlg.configure();
}
void
MainWindow::slotArchivePopup( const QPoint &pPoint)
{
static_cast<KPopupMenu *>(factory()->container("archive_popup", this))->popup(pPoint);
}
// see if the ark is already open in another window
bool
MainWindow::arkAlreadyOpen( const KURL & url )
{
if (ArkApplication::getInstance()->isArkOpenAlready(url))
{
if ( m_part->url() == url ) return true;
// raise the window containing the already open archive
ArkApplication::getInstance()->raiseArk(url);
// close this window
window_close();
// notify the user what's going on
KMessageBox::information(0, i18n("The archive %1 is already open and has been raised.\nNote: if the filename does not match, it only means that one of the two is a symbolic link.").arg(url.prettyURL()));
return true;
}
return false;
}
void
MainWindow::openURL( const KURL & url, bool tempFile )
{
if( !arkAlreadyOpen( url ) )
{
if ( tempFile && url.isLocalFile() )
m_widget->deleteAfterUse( url.path() );
m_part->openURL( url );
}
}
KURL
MainWindow::getOpenURL( bool addOnly, const QString & caption,
const QString & startDir, const QString & suggestedName )
{
kdDebug( 1601 ) << "startDir is: " << startDir << endl;
QWidget * forceFormatWidget = new QWidget( this );
QHBoxLayout * l = new QHBoxLayout( forceFormatWidget );
QLabel * label = new QLabel( forceFormatWidget );
label->setText( i18n( "Open &as:" ) );
label->adjustSize();
KComboBox * combo = new KComboBox( forceFormatWidget );
QStringList list;
list = ArchiveFormatInfo::self()->allDescriptions();
list.sort();
list.prepend( i18n( "Autodetect (default)" ) );
combo->insertStringList( list );
QString filter = ArchiveFormatInfo::self()->filter();
if ( !suggestedName.isEmpty() )
{
filter = QString::null;
combo->setCurrentItem( list.findIndex( ArchiveFormatInfo::self()->descriptionForMimeType(
KMimeType::findByPath( suggestedName, 0, true )->name() ) ) );
}
label->setBuddy( combo );
l->addWidget( label );
l->addWidget( combo, 1 );
QString dir;
if ( addOnly )
dir = startDir;
else
dir = ":ArkOpenDir";
KFileDialog dlg( dir, filter, this, "filedialog", true, forceFormatWidget );
dlg.setOperationMode( addOnly ? KFileDialog::Saving
: KFileDialog::Opening );
dlg.setCaption( addOnly ? caption : i18n("Open") );
dlg.setMode( addOnly ? ( KFile::File | KFile::ExistingOnly )
: KFile::File );
dlg.setSelection( suggestedName );
dlg.exec();
KURL url;
url = dlg.selectedURL();
if ( combo->currentItem() !=0 ) // i.e. != "Autodetect"
m_widget->setOpenAsMimeType(
ArchiveFormatInfo::self()->mimeTypeForDescription( combo->currentText() ) );
else
m_widget->setOpenAsMimeType( QString::null );
return url;
}
void
MainWindow::file_open()
{
KURL url = getOpenURL();
if( !arkAlreadyOpen( url ) )
m_part->openURL( url );
}
void
MainWindow::file_close()
{
m_part->closeURL();
}
void
MainWindow::window_close()
{
file_close();
slotSaveProperties();
//kdDebug(1601) << "-ArkWidget::window_close" << endl;
close();
}
bool
MainWindow::queryClose()
{
window_close();
return true;
}
void
MainWindow::file_quit()
{
window_close();
}
void
MainWindow::slotSaveProperties()
{
recent->saveEntries(kapp->config());
}
void
MainWindow::saveProperties( KConfig* config )
{
//TODO: make it work for URLS
config->writePathEntry( "SMOpenedFile",m_widget->getArchName() );
config->sync();
}
void
MainWindow::readProperties( KConfig* config )
{
QString file = config->readPathEntry("SMOpenedFile");
kdDebug(1601) << "ArkWidget::readProperties( KConfig* config ) file=" << file << endl;
if ( !file.isEmpty() )
openURL( KURL::fromPathOrURL( file ) );
}
void
MainWindow::slotAddRecentURL( const KURL & url )
{
recent->addURL( url );
recent->saveEntries(kapp->config());
}
void
MainWindow::slotRemoveRecentURL( const KURL & url )
{
recent->removeURL( url );
recent->saveEntries(kapp->config());
}
void
MainWindow::slotAddOpenArk( const KURL & _arkname )
{
ArkApplication::getInstance()->addOpenArk( _arkname, this );
}
void
MainWindow::slotRemoveOpenArk( const KURL & _arkname )
{
ArkApplication::getInstance()->removeOpenArk( _arkname );
}
void
MainWindow::setExtractOnly ( bool b )
{
m_widget->setExtractOnly( b );
}
void
MainWindow::extractTo( const KURL & targetDirectory, const KURL & archive, bool guessName )
{
startProgressDialog( i18n( "Extracting..." ) );
m_widget->extractTo( targetDirectory, archive, guessName );
m_part->openURL( archive );
}
void
MainWindow::addToArchive( const KURL::List & filesToAdd, const QString & /*cwd*/,
const KURL & archive, bool askForName )
{
KURL archiveFile;
if ( askForName || archive.isEmpty() )
{
// user definded actions in servicemus are being started by konq
// from konqis working directory, not from the one being shown when
// the popupmenu was requested; work around that so the user
// sees a list of the archives in the diretory he is looking at.
// makes it show the 'wrong' dir when being called from the commandline
// like: /dira> ark -add /dirb/file, but well...
KURL cwdURL;
cwdURL.setPath( filesToAdd.first().path() );
QString dir = cwdURL.directory( false );
archiveFile = getOpenURL( true, i18n( "Select Archive to Add Files To" ),
dir /*cwd*/, archive.fileName() );
}
else
archiveFile = archive;
if ( archiveFile.isEmpty() )
{
kdDebug( 1601 ) << "no archive selected." << endl;
file_quit();
return;
}
startProgressDialog( i18n( "Compressing..." ) );
bool exists = KIO::NetAccess::exists( archiveFile, false, m_widget );
kdDebug( 1601 ) << archiveFile << endl;
if ( !m_widget->addToArchive( filesToAdd, archiveFile ) )
file_quit();
if ( exists )
m_part->openURL( archiveFile );
}
void
MainWindow::startProgressDialog( const QString & text )
{
if ( !progressDialog )
progressDialog = new KProgressDialog( this, "progress_dialog", QString::null, text, false );
else
progressDialog->setLabel( text );
// progressDialog->setWFlags( Qt::WType_TopLevel );
progressDialog->setAllowCancel( true );
progressDialog->setPlainCaption( i18n( "Please Wait" ) );
progressDialog->progressBar()->setTotalSteps( 0 );
progressDialog->progressBar()->setPercentageVisible( false );
// progressDialog->setInitialSize( QSize(200,100), true );
progressDialog->setMinimumDuration( 500 );
progressDialog->show();
KDialog::centerOnScreen( progressDialog );
connect( progressDialog, SIGNAL( cancelClicked() ), this, SLOT( window_close() ) );
timer = new QTimer( this );
connect( timer, SIGNAL( timeout() ), this, SLOT( slotProgress() ) );
timer->start( 200, false );
}
void
MainWindow::slotProgress()
{
progressDialog->progressBar()->setProgress( progressDialog->progressBar()->progress() + 4 );
}
#include "mainwindow.moc"
|