00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <qfile.h>
00026 #include <qdir.h>
00027 #include <qdialog.h>
00028 #include <qimage.h>
00029 #include <qpixmap.h>
00030 #include <qlabel.h>
00031 #include <qlayout.h>
00032 #include <qpushbutton.h>
00033 #include <qtoolbutton.h>
00034 #include <qcheckbox.h>
00035 #include <qtooltip.h>
00036 #include <qstyle.h>
00037
00038 #include <kapplication.h>
00039 #include <kbuttonbox.h>
00040 #include <kcombobox.h>
00041 #include <kdesktopfile.h>
00042 #include <kdialog.h>
00043 #include <kglobal.h>
00044 #include <klineedit.h>
00045 #include <klocale.h>
00046 #include <kiconloader.h>
00047 #include <kmimemagic.h>
00048 #include <krun.h>
00049 #include <kstandarddirs.h>
00050 #include <kstringhandler.h>
00051 #include <kuserprofile.h>
00052 #include <kurlcompletion.h>
00053 #include <kurlrequester.h>
00054 #include <dcopclient.h>
00055 #include <kmimetype.h>
00056 #include <kservicegroup.h>
00057 #include <klistview.h>
00058 #include <ksycoca.h>
00059
00060 #include "kopenwith.h"
00061 #include "kopenwith_p.h"
00062
00063 #include <kdebug.h>
00064 #include <assert.h>
00065 #include <stdlib.h>
00066
00067 template class QPtrList<QString>;
00068
00069 #define SORT_SPEC (QDir::DirsFirst | QDir::Name | QDir::IgnoreCase)
00070
00071
00072
00073
00074 KAppTreeListItem::KAppTreeListItem( KListView* parent, const QString & name,
00075 const QPixmap& pixmap, bool parse, bool dir, QString p, QString c )
00076 : QListViewItem( parent, name )
00077 {
00078 init(pixmap, parse, dir, p, c);
00079 }
00080
00081
00082
00083
00084 KAppTreeListItem::KAppTreeListItem( QListViewItem* parent, const QString & name,
00085 const QPixmap& pixmap, bool parse, bool dir, QString p, QString c )
00086 : QListViewItem( parent, name )
00087 {
00088 init(pixmap, parse, dir, p, c);
00089 }
00090
00091
00092
00093
00094 void KAppTreeListItem::init(const QPixmap& pixmap, bool parse, bool dir, QString _path, QString _exec)
00095 {
00096 setPixmap(0, pixmap);
00097 parsed = parse;
00098 directory = dir;
00099 path = _path;
00100 exec = _exec;
00101 }
00102
00103
00104
00105
00106
00107 QString KAppTreeListItem::key(int column, bool ) const
00108 {
00109 if (directory)
00110 return QString::fromLatin1(" ") + text(column).upper();
00111 else
00112 return text(column).upper();
00113 }
00114
00115 void KAppTreeListItem::activate()
00116 {
00117 if ( directory )
00118 setOpen(!isOpen());
00119 }
00120
00121 void KAppTreeListItem::setOpen( bool o )
00122 {
00123 if( o && !parsed ) {
00124 ((KApplicationTree *) parent())->addDesktopGroup( path, this );
00125 parsed = true;
00126 }
00127 QListViewItem::setOpen( o );
00128 }
00129
00130 bool KAppTreeListItem::isDirectory()
00131 {
00132 return directory;
00133 }
00134
00135
00136
00137 KApplicationTree::KApplicationTree( QWidget *parent )
00138 : KListView( parent ), currentitem(0)
00139 {
00140 addColumn( i18n("Known Applications") );
00141 setRootIsDecorated( true );
00142
00143 addDesktopGroup( QString::null );
00144
00145 connect( this, SIGNAL( currentChanged(QListViewItem*) ),
00146 SLOT( slotItemHighlighted(QListViewItem*) ) );
00147 connect( this, SIGNAL( selectionChanged(QListViewItem*) ),
00148 SLOT( slotSelectionChanged(QListViewItem*) ) );
00149 }
00150
00151
00152
00153 bool KApplicationTree::isDirSel()
00154 {
00155 if (!currentitem) return false;
00156 return currentitem->isDirectory();
00157 }
00158
00159
00160
00161 static QPixmap appIcon(const QString &iconName)
00162 {
00163 QPixmap normal = KGlobal::iconLoader()->loadIcon(iconName, KIcon::Small, 0, KIcon::DefaultState, 0L, true);
00164
00165 if (normal.width() > 20 || normal.height() > 20)
00166 {
00167 QImage tmp = normal.convertToImage();
00168 tmp = tmp.smoothScale(20, 20);
00169 normal.convertFromImage(tmp);
00170 }
00171 return normal;
00172 }
00173
00174 void KApplicationTree::addDesktopGroup( QString relPath, KAppTreeListItem *item)
00175 {
00176 KServiceGroup::Ptr root = KServiceGroup::group(relPath);
00177 KServiceGroup::List list = root->entries();
00178
00179 KAppTreeListItem * newItem;
00180 for( KServiceGroup::List::ConstIterator it = list.begin();
00181 it != list.end(); it++)
00182 {
00183 QString icon;
00184 QString text;
00185 QString relPath;
00186 QString exec;
00187 bool isDir = false;
00188 KSycocaEntry *p = (*it);
00189 if (p->isType(KST_KService))
00190 {
00191 KService *service = static_cast<KService *>(p);
00192
00193 if (service->noDisplay())
00194 continue;
00195
00196 icon = service->icon();
00197 text = service->name();
00198 exec = service->exec();
00199 }
00200 else if (p->isType(KST_KServiceGroup))
00201 {
00202 KServiceGroup *serviceGroup = static_cast<KServiceGroup *>(p);
00203
00204 if (serviceGroup->noDisplay())
00205 continue;
00206
00207 icon = serviceGroup->icon();
00208 text = serviceGroup->caption();
00209 relPath = serviceGroup->relPath();
00210 isDir = true;
00211 if ( text[0] == '.' )
00212 continue;
00213 }
00214 else
00215 {
00216 kdWarning(250) << "KServiceGroup: Unexpected object in list!" << endl;
00217 continue;
00218 }
00219
00220 QPixmap pixmap = appIcon( icon );
00221
00222 if (item)
00223 newItem = new KAppTreeListItem( item, text, pixmap, false, isDir,
00224 relPath, exec );
00225 else
00226 newItem = new KAppTreeListItem( this, text, pixmap, false, isDir,
00227 relPath, exec );
00228 if (isDir)
00229 newItem->setExpandable( true );
00230 }
00231 }
00232
00233
00234
00235
00236 void KApplicationTree::slotItemHighlighted(QListViewItem* i)
00237 {
00238
00239 if(!i)
00240 return;
00241
00242 KAppTreeListItem *item = (KAppTreeListItem *) i;
00243
00244 currentitem = item;
00245
00246 if( (!item->directory ) && (!item->exec.isEmpty()) )
00247 emit highlighted( item->text(0), item->exec );
00248 }
00249
00250
00251
00252
00253 void KApplicationTree::slotSelectionChanged(QListViewItem* i)
00254 {
00255
00256 if(!i)
00257 return;
00258
00259 KAppTreeListItem *item = (KAppTreeListItem *) i;
00260
00261 currentitem = item;
00262
00263 if( ( !item->directory ) && (!item->exec.isEmpty() ) )
00264 emit selected( item->text(0), item->exec );
00265 }
00266
00267
00268
00269 void KApplicationTree::resizeEvent( QResizeEvent * e)
00270 {
00271 setColumnWidth(0, width()-QApplication::style().pixelMetric(QStyle::PM_ScrollBarExtent)
00272 -2*QApplication::style().pixelMetric(QStyle::PM_DefaultFrameWidth));
00273 KListView::resizeEvent(e);
00274 }
00275
00276
00277
00278
00279
00280
00281
00282
00283 KOpenWithDlg::KOpenWithDlg( const KURL::List& _urls, QWidget* parent )
00284 :QDialog( parent, 0L, true )
00285 {
00286 setCaption( i18n( "Open With" ) );
00287 QString text;
00288 if( _urls.count() == 1 )
00289 {
00290 text = i18n("<qt>Select the program that should be used to open <b>%1</b>. "
00291 "If the program is not listed, enter the name or click "
00292 "the browse button.</qt>").arg( _urls.first().fileName() );
00293 }
00294 else
00295
00296 text = i18n( "Choose the name of the program with which to open the selected files." );
00297 setServiceType( _urls );
00298 init( text, QString() );
00299 }
00300
00301 KOpenWithDlg::KOpenWithDlg( const KURL::List& _urls, const QString&_text,
00302 const QString& _value, QWidget *parent)
00303 :QDialog( parent, 0L, true )
00304 {
00305 QString caption = KStringHandler::csqueeze( _urls.first().prettyURL() );
00306 if (_urls.count() > 1)
00307 caption += QString::fromLatin1("...");
00308 setCaption(caption);
00309 setServiceType( _urls );
00310 init( _text, _value );
00311 }
00312
00313 KOpenWithDlg::KOpenWithDlg( const QString &serviceType, const QString& value,
00314 QWidget *parent)
00315 :QDialog( parent, 0L, true )
00316 {
00317 setCaption(i18n("Choose Application for %1").arg(serviceType));
00318 QString text = i18n("<qt>Select the program for the file type: <b>%1</b>. "
00319 "If the program is not listed, enter the name or click "
00320 "the browse button.</qt>").arg(serviceType);
00321 qServiceType = serviceType;
00322 init( text, value );
00323 if (remember)
00324 {
00325 remember->setChecked( true );
00326 remember->hide();
00327 }
00328 }
00329
00330 KOpenWithDlg::KOpenWithDlg( QWidget *parent)
00331 :QDialog( parent, 0L, true )
00332 {
00333 setCaption(i18n("Choose Application"));
00334 QString text = i18n("<qt>Select a program. "
00335 "If the program is not listed, enter the name or click "
00336 "the browse button.</qt>");
00337 qServiceType = QString::null;
00338 init( text, QString::null );
00339 }
00340
00341 void KOpenWithDlg::setServiceType( const KURL::List& _urls )
00342 {
00343 if ( _urls.count() == 1 )
00344 {
00345 qServiceType = KMimeType::findByURL( _urls.first())->name();
00346 if (qServiceType == QString::fromLatin1("application/octet-stream"))
00347 qServiceType = QString::null;
00348 }
00349 else
00350 qServiceType = QString::null;
00351 }
00352
00353 void KOpenWithDlg::init( const QString& _text, const QString& _value )
00354 {
00355 bool bReadOnly = kapp && !kapp->authorize("shell_access");
00356 m_terminaldirty = false;
00357 m_pTree = 0L;
00358 m_pService = 0L;
00359
00360 QBoxLayout *topLayout = new QVBoxLayout( this, KDialog::marginHint(),
00361 KDialog::spacingHint() );
00362 label = new QLabel( _text, this );
00363 topLayout->addWidget(label);
00364
00365 QHBoxLayout* hbox = new QHBoxLayout(topLayout);
00366
00367 QToolButton *clearButton = new QToolButton( this );
00368 clearButton->setIconSet( BarIcon( "locationbar_erase" ) );
00369 clearButton->setFixedSize( clearButton->sizeHint() );
00370 connect( clearButton, SIGNAL( pressed() ), SLOT( slotClear() ) );
00371 QToolTip::add( clearButton, i18n( "Clear input field" ) );
00372
00373 hbox->addWidget( clearButton );
00374
00375 if (!bReadOnly)
00376 {
00377
00378 KHistoryCombo *combo = new KHistoryCombo();
00379 combo->setDuplicatesEnabled( false );
00380 KConfig *kc = KGlobal::config();
00381 KConfigGroupSaver ks( kc, QString::fromLatin1("Open-with settings") );
00382 int max = kc->readNumEntry( QString::fromLatin1("Maximum history"), 15 );
00383 combo->setMaxCount( max );
00384 int mode = kc->readNumEntry(QString::fromLatin1("CompletionMode"),
00385 KGlobalSettings::completionMode());
00386 combo->setCompletionMode((KGlobalSettings::Completion)mode);
00387 QStringList list = kc->readListEntry( QString::fromLatin1("History") );
00388 combo->setHistoryItems( list, true );
00389 edit = new KURLRequester( combo, this );
00390 }
00391 else
00392 {
00393 clearButton->hide();
00394 edit = new KURLRequester( this );
00395 edit->lineEdit()->setReadOnly(true);
00396 edit->button()->hide();
00397 }
00398
00399 edit->setURL( _value );
00400
00401 hbox->addWidget(edit);
00402
00403 if ( edit->comboBox() ) {
00404 KURLCompletion *comp = new KURLCompletion( KURLCompletion::ExeCompletion );
00405 edit->comboBox()->setCompletionObject( comp );
00406 }
00407
00408 connect ( edit, SIGNAL(returnPressed()), SLOT(slotOK()) );
00409 connect ( edit, SIGNAL(textChanged(const QString&)), SLOT(slotTextChanged()) );
00410
00411 m_pTree = new KApplicationTree( this );
00412 topLayout->addWidget(m_pTree);
00413
00414 connect( m_pTree, SIGNAL( selected( const QString&, const QString& ) ),
00415 SLOT( slotSelected( const QString&, const QString& ) ) );
00416 connect( m_pTree, SIGNAL( highlighted( const QString&, const QString& ) ),
00417 SLOT( slotHighlighted( const QString&, const QString& ) ) );
00418 connect( m_pTree, SIGNAL( doubleClicked(QListViewItem*) ),
00419 SLOT( slotDbClick() ) );
00420
00421 terminal = new QCheckBox( i18n("Run in &terminal"), this );
00422 if (bReadOnly)
00423 terminal->hide();
00424 connect(terminal, SIGNAL(toggled(bool)), SLOT(slotTerminalToggled(bool)));
00425
00426 topLayout->addWidget(terminal);
00427
00428 if (!qServiceType.isNull())
00429 {
00430 remember = new QCheckBox(i18n("&Remember application association for this type of file"), this);
00431
00432 topLayout->addWidget(remember);
00433 }
00434 else
00435 remember = 0L;
00436
00437
00438 KButtonBox* b = new KButtonBox( this );
00439 b->addStretch( 2 );
00440
00441 QPushButton* ok = b->addButton( i18n ( "&OK" ) );
00442 ok->setDefault( true );
00443 connect( ok, SIGNAL( clicked() ), SLOT( slotOK() ) );
00444
00445 QPushButton* cancel = b->addButton( i18n( "&Cancel" ) );
00446 connect( cancel, SIGNAL( clicked() ), SLOT( reject() ) );
00447
00448 b->layout();
00449 topLayout->addWidget( b );
00450
00451
00452
00453
00454
00455 edit->setFocus();
00456 }
00457
00458
00459
00460
00461 KOpenWithDlg::~KOpenWithDlg()
00462 {
00463 }
00464
00465
00466
00467 void KOpenWithDlg::slotClear()
00468 {
00469 edit->setURL(QString::null);
00470 edit->setFocus();
00471 }
00472
00473
00474
00475
00476 void KOpenWithDlg::slotSelected( const QString& , const QString& _exec )
00477 {
00478 kdDebug(250)<<"KOpenWithDlg::slotSelected"<<endl;
00479 KService::Ptr pService = m_pService;
00480 edit->setURL( _exec );
00481 m_pService = pService;
00482 }
00483
00484
00485
00486
00487 void KOpenWithDlg::slotHighlighted( const QString& _name, const QString& )
00488 {
00489 kdDebug(250)<<"KOpenWithDlg::slotHighlighted"<<endl;
00490 qName = _name;
00491 m_pService = KService::serviceByName( qName );
00492 if (!m_terminaldirty)
00493 {
00494
00495 terminal->setChecked(m_pService->terminal());
00496 m_terminaldirty = false;
00497 }
00498 }
00499
00500
00501
00502 void KOpenWithDlg::slotTextChanged()
00503 {
00504 kdDebug(250)<<"KOpenWithDlg::slotTextChanged"<<endl;
00505
00506 m_pService = 0L;
00507 }
00508
00509
00510
00511 void KOpenWithDlg::slotTerminalToggled(bool)
00512 {
00513
00514 m_terminaldirty = true;
00515 }
00516
00517
00518
00519 void KOpenWithDlg::slotDbClick()
00520 {
00521 if (m_pTree->isDirSel() ) return;
00522 slotOK();
00523 }
00524
00525 void KOpenWithDlg::slotOK()
00526 {
00527 QString fullExec(edit->url());
00528
00529 QString serviceName;
00530 QString pathName;
00531 QString initialServiceName;
00532 if (!m_pService) {
00533
00534
00535
00536 serviceName = KRun::binaryName( fullExec, true );
00537 if (serviceName.isEmpty())
00538 {
00539
00540 return;
00541 }
00542 initialServiceName = serviceName;
00543 int i = 1;
00544
00545 bool ok = false;
00546 do {
00547 KService::Ptr serv = KService::serviceByDesktopName( serviceName );
00548 ok = !serv;
00549
00550 if ( serv &&
00551 serv->type() == "Application" && (
00552 serv->exec() == fullExec ||
00553 serv->exec() == fullExec + " %u" ||
00554 serv->exec() == fullExec + " %U" ||
00555 serv->exec() == fullExec + " %f" ||
00556 serv->exec() == fullExec + " %F"
00557 ) )
00558 {
00559 ok = true;
00560 m_pService = serv;
00561 }
00562 if (!ok)
00563 {
00564 ++i;
00565 serviceName = initialServiceName + "-" + QString::number(i);
00566 }
00567 }
00568 while (!ok);
00569 if ( !m_pService )
00570 {
00571
00572 pathName = ".hidden/";
00573 pathName += serviceName;
00574 }
00575 }
00576 if ( m_pService )
00577 {
00578
00579 serviceName = m_pService->name();
00580 initialServiceName = serviceName;
00581 pathName = m_pService->desktopEntryPath();
00582 }
00583
00584 if (terminal->isChecked()) {
00585 KSimpleConfig conf(QString::fromLatin1("konquerorrc"), true);
00586 conf.setGroup(QString::fromLatin1("Misc Defaults"));
00587 m_command = conf.readPathEntry(QString::fromLatin1("Terminal"), QString::fromLatin1("konsole"));
00588
00589 m_command += QString::fromLatin1(" -e ");
00590 m_command += edit->url();
00591 kdDebug(250) << "Setting m_command to " << m_command << endl;
00592 }
00593 if ( m_pService && terminal->isChecked() != m_pService->terminal() )
00594 m_pService = 0L;
00595
00596 if ( m_pService && ( !remember || !remember->isChecked() ) ) {
00597 accept();
00598 return;
00599 }
00600
00601
00602
00603
00604
00605 kdDebug(250) << "service pathName=" << pathName << endl;
00606 if (pathName.right(8) != QString::fromLatin1(".desktop"))
00607 pathName += QString::fromLatin1(".desktop");
00608 QString path(locateLocal("apps", pathName));
00609
00610 int maxPreference = 1;
00611 if (!qServiceType.isEmpty())
00612 {
00613 KServiceTypeProfile::OfferList offerList = KServiceTypeProfile::offers( qServiceType );
00614 if (!offerList.isEmpty())
00615 maxPreference = offerList.first().preference();
00616 }
00617
00618 KDesktopFile desktop(path);
00619 desktop.writeEntry(QString::fromLatin1("Type"), QString::fromLatin1("Application"));
00620 desktop.writeEntry(QString::fromLatin1("Name"), initialServiceName);
00621 desktop.writeEntry(QString::fromLatin1("Exec"), fullExec);
00622 desktop.writeEntry(QString::fromLatin1("InitialPreference"), maxPreference + 1);
00623 if (remember)
00624 if (remember->isChecked()) {
00625 QStringList mimeList;
00626 KDesktopFile oldDesktop(locate("apps", pathName), true);
00627 mimeList = oldDesktop.readListEntry(QString::fromLatin1("MimeType"), ';');
00628 if (!qServiceType.isEmpty() && !mimeList.contains(qServiceType))
00629 mimeList.append(qServiceType);
00630 desktop.writeEntry(QString::fromLatin1("MimeType"), mimeList, ';');
00631 if (terminal->isChecked())
00632 desktop.writeEntry(QString::fromLatin1("Terminal"), true);
00633 else
00634 desktop.writeEntry(QString::fromLatin1("Terminal"), false);
00635
00636 if ( !qServiceType.isEmpty() )
00637 {
00638
00639 KDesktopFile mimeDesktop( locateLocal( "mime", qServiceType + ".desktop" ) );
00640 mimeDesktop.writeEntry( QString::fromLatin1( "X-KDE-AutoEmbed" ), false );
00641 mimeDesktop.sync();
00642 }
00643 }
00644
00645
00646
00647 desktop.sync();
00648
00649 QApplication::setOverrideCursor( waitCursor );
00650
00651
00652 QStringList args;
00653 args.append("--incremental");
00654 KApplication::kdeinitExecWait( "kbuildsycoca", args );
00655
00656
00657 kdDebug(250) << "kbuildsycoca finished, looking for service " << pathName << endl;
00658
00659
00660
00661 QStringList lst;
00662 lst << QString::fromLatin1("apps");
00663 KSycoca::self()->notifyDatabaseChanged( lst );
00664
00665 m_pService = KService::serviceByDesktopPath( pathName );
00666 QApplication::restoreOverrideCursor();
00667
00668 Q_ASSERT( m_pService );
00669
00670 accept();
00671 }
00672
00673 QString KOpenWithDlg::text() const
00674 {
00675 if (!m_command.isEmpty())
00676 return m_command;
00677 else
00678 return edit->url();
00679 }
00680
00681 void KOpenWithDlg::accept()
00682 {
00683 KHistoryCombo *combo = static_cast<KHistoryCombo*>( edit->comboBox() );
00684 if ( combo ) {
00685 combo->addToHistory( edit->url() );
00686
00687 KConfig *kc = KGlobal::config();
00688 KConfigGroupSaver ks( kc, QString::fromLatin1("Open-with settings") );
00689 kc->writeEntry( QString::fromLatin1("History"), combo->historyItems() );
00690 kc->writeEntry(QString::fromLatin1("CompletionMode"),
00691 combo->completionMode());
00692
00693
00694 kc->sync();
00695 }
00696
00697 QDialog::accept();
00698 }
00699
00700
00702
00703 #ifndef KDE_NO_COMPAT
00704 bool KFileOpenWithHandler::displayOpenWithDialog( const KURL::List& urls )
00705 {
00706 KOpenWithDlg l( urls, i18n("Open with:"), QString::null, 0L );
00707 if ( l.exec() )
00708 {
00709 KService::Ptr service = l.service();
00710 if ( !!service )
00711 return KRun::run( *service, urls );
00712
00713 kdDebug(250) << "No service set, running " << l.text() << endl;
00714 return KRun::run( l.text(), urls );
00715 }
00716 return false;
00717 }
00718 #endif
00719
00720 #include "kopenwith.moc"
00721 #include "kopenwith_p.moc"
00722