• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdefile
 

tdeio/tdefile

  • tdeio
  • tdefile
kopenwith.cpp
1 /* This file is part of the KDE libraries
2 
3  Copyright (C) 1997 Torben Weis <weis@stud.uni-frankfurt.de>
4  Copyright (C) 1999 Dirk Mueller <mueller@kde.org>
5  Portions copyright (C) 1999 Preston Brown <pbrown@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library 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 GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include <tqfile.h>
24 #include <tqdir.h>
25 #include <tqdialog.h>
26 #include <tqimage.h>
27 #include <tqpixmap.h>
28 #include <tqlabel.h>
29 #include <tqlayout.h>
30 #include <tqpushbutton.h>
31 #include <tqtoolbutton.h>
32 #include <tqcheckbox.h>
33 #include <tqtooltip.h>
34 #include <tqstyle.h>
35 #include <tqwhatsthis.h>
36 
37 #include <tdeapplication.h>
38 #include <kbuttonbox.h>
39 #include <kcombobox.h>
40 #include <kdesktopfile.h>
41 #include <kdialog.h>
42 #include <tdeglobal.h>
43 #include <klineedit.h>
44 #include <tdelocale.h>
45 #include <kiconloader.h>
46 #include <kmimemagic.h>
47 #include <krun.h>
48 #include <kstandarddirs.h>
49 #include <kstringhandler.h>
50 #include <kuserprofile.h>
51 #include <kurlcompletion.h>
52 #include <kurlrequester.h>
53 #include <dcopclient.h>
54 #include <kmimetype.h>
55 #include <kservicegroup.h>
56 #include <tdelistview.h>
57 #include <tdesycoca.h>
58 #include <kstdguiitem.h>
59 
60 #include "kopenwith.h"
61 #include "kopenwith_p.h"
62 
63 #include <kdebug.h>
64 #include <assert.h>
65 #include <stdlib.h>
66 
67 #define SORT_SPEC (TQDir::DirsFirst | TQDir::Name | TQDir::IgnoreCase)
68 
69 
70 // ----------------------------------------------------------------------
71 
72 KAppTreeListItem::KAppTreeListItem( TDEListView* parent, const TQString & name,
73  const TQPixmap& pixmap, bool parse, bool dir,
74  const TQString &p, const TQString &c, const TQString &dp )
75  : TQListViewItem( parent, name )
76 {
77  init(pixmap, parse, dir, p, c, dp);
78 }
79 
80 
81 // ----------------------------------------------------------------------
82 
83 KAppTreeListItem::KAppTreeListItem( TQListViewItem* parent, const TQString & name,
84  const TQPixmap& pixmap, bool parse, bool dir,
85  const TQString &p, const TQString &c, const TQString &dp )
86  : TQListViewItem( parent, name )
87 {
88  init(pixmap, parse, dir, p, c, dp);
89 }
90 
91 
92 // ----------------------------------------------------------------------
93 
94 void KAppTreeListItem::init(const TQPixmap& pixmap, bool parse, bool dir,
95  const TQString &_path, const TQString &_exec, const TQString &_desktopPath)
96 {
97  setPixmap(0, pixmap);
98  parsed = parse;
99  directory = dir;
100  path = _path; // relative path
101  exec = _exec; // executable command
102  desktopPath = _desktopPath; // .desktop file path
103 }
104 
105 
106 /* Ensures that directories sort before non-directories */
107 int KAppTreeListItem::compare(TQListViewItem *i, int col, bool ascending) const
108 {
109  KAppTreeListItem *other = dynamic_cast<KAppTreeListItem *>(i);
110 
111  // Directories sort first
112  if (directory && !other->directory)
113  return -1;
114 
115  else if (!directory && other->directory)
116  return 1;
117 
118  else // both directories or both not
119  return TQListViewItem::compare(i, col, ascending);
120 }
121 
122 // ----------------------------------------------------------------------
123 // Ensure that case is ignored
124 TQString KAppTreeListItem::key(int column, bool /*ascending*/) const
125 {
126  return text(column).upper();
127 }
128 
129 void KAppTreeListItem::activate()
130 {
131  if ( directory )
132  setOpen(!isOpen());
133 }
134 
135 void KAppTreeListItem::setOpen( bool o )
136 {
137  if( o && !parsed ) { // fill the children before opening
138  ((TDEApplicationTree *) parent())->addDesktopGroup( path, this );
139  parsed = true;
140  }
141  TQListViewItem::setOpen( o );
142 }
143 
144 bool KAppTreeListItem::isDirectory()
145 {
146  return directory;
147 }
148 
149 // ----------------------------------------------------------------------
150 
151 TDEApplicationTree::TDEApplicationTree( TQWidget *parent )
152  : TDEListView( parent ), currentitem(0)
153 {
154  addColumn( i18n("Known Applications") );
155  setRootIsDecorated( true );
156 
157  addDesktopGroup( TQString::null );
158  cleanupTree();
159 
160  connect( this, TQ_SIGNAL( currentChanged(TQListViewItem*) ),
161  TQ_SLOT( slotItemHighlighted(TQListViewItem*) ) );
162  connect( this, TQ_SIGNAL( selectionChanged(TQListViewItem*) ),
163  TQ_SLOT( slotSelectionChanged(TQListViewItem*) ) );
164 }
165 
166 // ----------------------------------------------------------------------
167 
168 bool TDEApplicationTree::isDirSel()
169 {
170  if (!currentitem) return false; // if currentitem isn't set
171  return currentitem->isDirectory();
172 }
173 
174 // ----------------------------------------------------------------------
175 
176 static TQPixmap appIcon(const TQString &iconName)
177 {
178  TQPixmap normal = TDEGlobal::iconLoader()->loadIcon(iconName, TDEIcon::Small, 0, TDEIcon::DefaultState, 0L, true);
179  // make sure they are not larger than 20x20
180  if (normal.width() > 20 || normal.height() > 20)
181  {
182  TQImage tmp = normal.convertToImage();
183  tmp = tmp.smoothScale(20, 20);
184  normal.convertFromImage(tmp);
185  }
186  return normal;
187 }
188 
189 void TDEApplicationTree::addDesktopGroup( const TQString &relPath, KAppTreeListItem *item)
190 {
191  KServiceGroup::Ptr root = KServiceGroup::group(relPath);
192  if (!root || !root->isValid()) return;
193 
194  KServiceGroup::List list = root->entries();
195 
196  KAppTreeListItem * newItem;
197  for( KServiceGroup::List::ConstIterator it = list.begin();
198  it != list.end(); it++)
199  {
200  TQString icon, text, relPath, exec, desktopPath;
201  bool isDir = false;
202  KSycocaEntry *p = (*it);
203  if (p->isType(KST_KService))
204  {
205  KService *service = static_cast<KService *>(p);
206 
207  if (service->noDisplay())
208  continue;
209 
210  icon = service->icon();
211  text = service->name();
212  exec = service->exec();
213  desktopPath = service->desktopEntryPath();
214  }
215  else if (p->isType(KST_KServiceGroup))
216  {
217  KServiceGroup *serviceGroup = static_cast<KServiceGroup *>(p);
218 
219  if (serviceGroup->noDisplay() || serviceGroup->childCount() == 0)
220  continue;
221 
222  icon = serviceGroup->icon();
223  text = serviceGroup->caption();
224  relPath = serviceGroup->relPath();
225  isDir = true;
226  }
227  else
228  {
229  kdWarning(250) << "KServiceGroup: Unexpected object in list!" << endl;
230  continue;
231  }
232 
233  TQPixmap pixmap = appIcon( icon );
234 
235  if (item)
236  newItem = new KAppTreeListItem( item, text, pixmap, false, isDir,
237  relPath, exec, desktopPath );
238  else
239  newItem = new KAppTreeListItem( this, text, pixmap, false, isDir,
240  relPath, exec, desktopPath );
241  if (isDir)
242  newItem->setExpandable( true );
243  }
244 }
245 
246 
247 // ----------------------------------------------------------------------
248 
249 void TDEApplicationTree::slotItemHighlighted(TQListViewItem* i)
250 {
251  // i may be 0 (see documentation)
252  if(!i)
253  return;
254 
255  KAppTreeListItem *item = (KAppTreeListItem *) i;
256 
257  currentitem = item;
258 
259  if( (!item->directory ) && (!item->exec.isEmpty()) )
260  emit highlighted( item->text(0), item->exec, item->desktopPath );
261 }
262 
263 
264 // ----------------------------------------------------------------------
265 
266 void TDEApplicationTree::slotSelectionChanged(TQListViewItem* i)
267 {
268  // i may be 0 (see documentation)
269  if(!i)
270  return;
271 
272  KAppTreeListItem *item = (KAppTreeListItem *) i;
273 
274  currentitem = item;
275 
276  if( ( !item->directory ) && (!item->exec.isEmpty() ) )
277  emit selected( item->text(0), item->exec, item->desktopPath );
278 }
279 
280 // ----------------------------------------------------------------------
281 
282 void TDEApplicationTree::resizeEvent( TQResizeEvent * e)
283 {
284  setColumnWidth(0, width()-TQApplication::style().pixelMetric(TQStyle::PM_ScrollBarExtent)
285  -2*TQApplication::style().pixelMetric(TQStyle::PM_DefaultFrameWidth));
286  TDEListView::resizeEvent(e);
287 }
288 
289 // Prune empty directories from the tree
290 void TDEApplicationTree::cleanupTree()
291 {
292  TQListViewItem *item=firstChild();
293  while(item!=0)
294  {
295  if(item->isExpandable())
296  {
297  TQListViewItem *temp=item->itemBelow();
298  if(item->text(0)!=i18n("Applications"))
299  item->setOpen(false);
300  item=temp;
301  continue;
302  }
303  item=item->itemBelow();
304  }
305 }
306 
307 /***************************************************************
308  *
309  * KOpenWithDlg
310  *
311  ***************************************************************/
312 class KOpenWithDlgPrivate
313 {
314 public:
315  KOpenWithDlgPrivate() : saveNewApps(false) { };
316  TQPushButton* ok;
317  bool saveNewApps;
318  KService::Ptr curService;
319 };
320 
321 KOpenWithDlg::KOpenWithDlg( const KURL::List& _urls, TQWidget* parent )
322  :TQDialog( parent, "openwith", true )
323 {
324  setCaption( i18n( "Open With" ) );
325  TQString text;
326  if( _urls.count() == 1 )
327  {
328  text = i18n("<qt>Select the program that should be used to open <b>%1</b>. "
329  "If the program is not listed, enter the name or click "
330  "the browse button.</qt>").arg( _urls.first().fileName() );
331  }
332  else
333  // Should never happen ??
334  text = i18n( "Choose the name of the program with which to open the selected files." );
335  setServiceType( _urls );
336  init( text, TQString() );
337 }
338 
339 KOpenWithDlg::KOpenWithDlg( const KURL::List& _urls, const TQString&_text,
340  const TQString& _value, TQWidget *parent)
341  :TQDialog( parent, "openwith", true )
342 {
343  TQString caption = KStringHandler::csqueeze( _urls.first().prettyURL() );
344  if (_urls.count() > 1)
345  caption += TQString::fromLatin1("...");
346  setCaption(caption);
347  setServiceType( _urls );
348  init( _text, _value );
349 }
350 
351 KOpenWithDlg::KOpenWithDlg( const TQString &serviceType, const TQString& value,
352  TQWidget *parent)
353  :TQDialog( parent, "openwith", true )
354 {
355  setCaption(i18n("Choose Application for %1").arg(serviceType));
356  TQString text = i18n("<qt>Select the program for the file type: <b>%1</b>. "
357  "If the program is not listed, enter the name or click "
358  "the browse button.</qt>").arg(serviceType);
359  qServiceType = serviceType;
360  init( text, value );
361  if (remember)
362  remember->hide();
363 }
364 
365 KOpenWithDlg::KOpenWithDlg( TQWidget *parent)
366  :TQDialog( parent, "openwith", true )
367 {
368  setCaption(i18n("Choose Application"));
369  TQString text = i18n("<qt>Select a program. "
370  "If the program is not listed, enter the name or click "
371  "the browse button.</qt>");
372  qServiceType = TQString::null;
373  init( text, TQString::null );
374 }
375 
376 void KOpenWithDlg::setServiceType( const KURL::List& _urls )
377 {
378  if ( _urls.count() == 1 )
379  {
380  qServiceType = KMimeType::findByURL( _urls.first())->name();
381  if (qServiceType == TQString::fromLatin1("application/octet-stream"))
382  qServiceType = TQString::null;
383  }
384  else
385  qServiceType = TQString::null;
386 }
387 
388 void KOpenWithDlg::init( const TQString& _text, const TQString& _value )
389 {
390  d = new KOpenWithDlgPrivate;
391  bool bReadOnly = kapp && !kapp->authorize("shell_access");
392  m_terminaldirty = false;
393  m_pTree = 0L;
394  m_pService = 0L;
395  d->curService = 0L;
396 
397  TQBoxLayout *topLayout = new TQVBoxLayout( this, KDialog::marginHint(),
398  KDialog::spacingHint() );
399  label = new TQLabel( _text, this );
400  topLayout->addWidget(label);
401 
402  TQHBoxLayout* hbox = new TQHBoxLayout(topLayout);
403 
404  TQToolButton *clearButton = new TQToolButton( this );
405  clearButton->setIconSet( BarIcon( "locationbar_erase" ) );
406  clearButton->setFixedSize( clearButton->sizeHint() );
407  connect( clearButton, TQ_SIGNAL( clicked() ), TQ_SLOT( slotClear() ) );
408  TQToolTip::add( clearButton, i18n( "Clear input field" ) );
409 
410  hbox->addWidget( clearButton );
411 
412  if (!bReadOnly)
413  {
414  // init the history combo and insert it into the URL-Requester
415  KHistoryCombo *combo = new KHistoryCombo();
416  combo->setDuplicatesEnabled( false );
417  TDEConfig *kc = TDEGlobal::config();
418  TDEConfigGroupSaver ks( kc, TQString::fromLatin1("Open-with settings") );
419  int max = kc->readNumEntry( TQString::fromLatin1("Maximum history"), 15 );
420  combo->setMaxCount( max );
421  int mode = kc->readNumEntry(TQString::fromLatin1("CompletionMode"),
422  TDEGlobalSettings::completionMode());
423  combo->setCompletionMode((TDEGlobalSettings::Completion)mode);
424  TQStringList list = kc->readListEntry( TQString::fromLatin1("History") );
425  combo->setHistoryItems( list, true );
426  edit = new KURLRequester( combo, this );
427  }
428  else
429  {
430  clearButton->hide();
431  edit = new KURLRequester( this );
432  edit->lineEdit()->setReadOnly(true);
433  edit->button()->hide();
434  }
435 
436  edit->setURL( _value );
437  TQWhatsThis::add(edit,i18n(
438  "Following the command, you can have several place holders which will be replaced "
439  "with the actual values when the actual program is run:\n"
440  "%f - a single file name\n"
441  "%F - a list of files; use for applications that can open several local files at once\n"
442  "%u - a single URL\n"
443  "%U - a list of URLs\n"
444  "%d - the directory of the file to open\n"
445  "%D - a list of directories\n"
446  "%i - the icon\n"
447  "%m - the mini-icon\n"
448  "%c - the comment"));
449 
450  hbox->addWidget(edit);
451 
452  if ( edit->comboBox() ) {
453  KURLCompletion *comp = new KURLCompletion( KURLCompletion::ExeCompletion );
454  edit->comboBox()->setCompletionObject( comp );
455  edit->comboBox()->setAutoDeleteCompletionObject( true );
456  }
457 
458  connect ( edit, TQ_SIGNAL(returnPressed()), TQ_SLOT(slotOK()) );
459  connect ( edit, TQ_SIGNAL(textChanged(const TQString&)), TQ_SLOT(slotTextChanged()) );
460 
461  m_pTree = new TDEApplicationTree( this );
462  topLayout->addWidget(m_pTree);
463 
464  connect( m_pTree, TQ_SIGNAL( selected( const TQString&, const TQString&, const TQString& ) ),
465  TQ_SLOT( slotSelected( const TQString&, const TQString&, const TQString& ) ) );
466  connect( m_pTree, TQ_SIGNAL( highlighted( const TQString&, const TQString&, const TQString& ) ),
467  TQ_SLOT( slotHighlighted( const TQString&, const TQString&, const TQString& ) ) );
468  connect( m_pTree, TQ_SIGNAL( doubleClicked(TQListViewItem*) ),
469  TQ_SLOT( slotDbClick() ) );
470 
471  terminal = new TQCheckBox( i18n("Run in &terminal"), this );
472  if (bReadOnly)
473  terminal->hide();
474  connect(terminal, TQ_SIGNAL(toggled(bool)), TQ_SLOT(slotTerminalToggled(bool)));
475 
476  topLayout->addWidget(terminal);
477 
478  TQBoxLayout* nocloseonexitLayout = new TQHBoxLayout( 0, 0, KDialog::spacingHint() );
479  TQSpacerItem* spacer = new TQSpacerItem( 20, 0, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
480  nocloseonexitLayout->addItem( spacer );
481 
482  nocloseonexit = new TQCheckBox( i18n("&Do not close when command exits"), this );
483  nocloseonexit->setChecked( false );
484  nocloseonexit->setDisabled( true );
485 
486  // check to see if we use konsole if not disable the nocloseonexit
487  // because we don't know how to do this on other terminal applications
488  TDEConfigGroup confGroup( TDEGlobal::config(), TQString::fromLatin1("General") );
489  TQString preferredTerminal = confGroup.readPathEntry("TerminalApplication", TQString::fromLatin1("konsole"));
490 
491  if (bReadOnly || preferredTerminal != "konsole")
492  nocloseonexit->hide();
493 
494  nocloseonexitLayout->addWidget( nocloseonexit );
495  topLayout->addLayout( nocloseonexitLayout );
496 
497  if (!qServiceType.isNull())
498  {
499  remember = new TQCheckBox(i18n("&Remember application association for this type of file"), this);
500  // remember->setChecked(true);
501  topLayout->addWidget(remember);
502  }
503  else
504  remember = 0L;
505 
506  // Use KButtonBox for the aligning pushbuttons nicely
507  KButtonBox* b = new KButtonBox( this );
508  b->addStretch( 2 );
509 
510  d->ok = b->addButton( KStdGuiItem::ok() );
511  d->ok->setDefault( true );
512  connect( d->ok, TQ_SIGNAL( clicked() ), TQ_SLOT( slotOK() ) );
513 
514  TQPushButton* cancel = b->addButton( KStdGuiItem::cancel() );
515  connect( cancel, TQ_SIGNAL( clicked() ), TQ_SLOT( reject() ) );
516 
517  b->layout();
518  topLayout->addWidget( b );
519 
520  //edit->setText( _value );
521  // This is what caused "can't click on items before clicking on Name header".
522  // Probably due to the resizeEvent handler using width().
523  //resize( minimumWidth(), sizeHint().height() );
524  edit->setFocus();
525  slotTextChanged();
526 }
527 
528 
529 // ----------------------------------------------------------------------
530 
531 KOpenWithDlg::~KOpenWithDlg()
532 {
533  delete d;
534  d = 0;
535 }
536 
537 // ----------------------------------------------------------------------
538 
539 void KOpenWithDlg::slotClear()
540 {
541  edit->setURL(TQString::null);
542  edit->setFocus();
543 }
544 
545 
546 // ----------------------------------------------------------------------
547 
548 void KOpenWithDlg::slotSelected( const TQString& /*_name*/, const TQString& _exec, const TQString& /*_desktopPath*/ )
549 {
550  kdDebug(250)<<"KOpenWithDlg::slotSelected"<<endl;
551  KService::Ptr pService = d->curService;
552  edit->setURL( _exec ); // calls slotTextChanged :(
553  d->curService = pService;
554 }
555 
556 
557 // ----------------------------------------------------------------------
558 
559 void KOpenWithDlg::slotHighlighted( const TQString& _name, const TQString& _exec, const TQString& _desktopPath )
560 {
561  kdDebug(250)<<"KOpenWithDlg::slotHighlighted"<<endl;
562  qName = _name;
563  // Look up by desktop path first, to avoid returning wrong results in case multiple .desktop files
564  // contain the same name for different services (such as Konsole)
565  // Try by name only if first search fails (this should never happen normally)
566  d->curService = KService::serviceByDesktopPath( _desktopPath );
567  if (!d->curService)
568  {
569  d->curService = KService::serviceByName( qName );
570  }
571  if (!m_terminaldirty)
572  {
573  // ### indicate that default value was restored
574  terminal->setChecked(d->curService->terminal());
575  TQString terminalOptions = d->curService->terminalOptions();
576  nocloseonexit->setChecked( (terminalOptions.contains( "--noclose" ) > 0) );
577  m_terminaldirty = false; // slotTerminalToggled changed it
578  }
579 }
580 
581 // ----------------------------------------------------------------------
582 
583 void KOpenWithDlg::slotTextChanged()
584 {
585  kdDebug(250)<<"KOpenWithDlg::slotTextChanged"<<endl;
586  // Forget about the service
587  d->curService = 0L;
588  d->ok->setEnabled( !edit->url().isEmpty());
589 }
590 
591 // ----------------------------------------------------------------------
592 
593 void KOpenWithDlg::slotTerminalToggled(bool)
594 {
595  // ### indicate that default value was overridden
596  m_terminaldirty = true;
597  nocloseonexit->setDisabled( ! terminal->isChecked() );
598 }
599 
600 // ----------------------------------------------------------------------
601 
602 void KOpenWithDlg::slotDbClick()
603 {
604  if (m_pTree->isDirSel() ) return; // check if a directory is selected
605  slotOK();
606 }
607 
608 void KOpenWithDlg::setSaveNewApplications(bool b)
609 {
610  d->saveNewApps = b;
611 }
612 
613 void KOpenWithDlg::slotOK()
614 {
615  TQString typedExec(edit->url());
616  TQString fullExec(typedExec);
617 
618  TQString serviceName;
619  TQString initialServiceName;
620  TQString preferredTerminal;
621  m_pService = d->curService;
622  if (!m_pService) {
623  // No service selected - check the command line
624 
625  // Find out the name of the service from the command line, removing args and paths
626  serviceName = KRun::binaryName( typedExec, true );
627  if (serviceName.isEmpty())
628  {
629  // TODO add a KMessageBox::error here after the end of the message freeze
630  return;
631  }
632  initialServiceName = serviceName;
633  kdDebug(250) << "initialServiceName=" << initialServiceName << endl;
634  int i = 1; // We have app, app-2, app-3... Looks better for the user.
635  bool ok = false;
636  // Check if there's already a service by that name, with the same Exec line
637  do {
638  kdDebug(250) << "looking for service " << serviceName << endl;
639  KService::Ptr serv = KService::serviceByDesktopName( serviceName );
640  ok = !serv; // ok if no such service yet
641  // also ok if we find the exact same service (well, "kwrite" == "kwrite %U"
642  if ( serv && serv->type() == "Application")
643  {
644  TQString exec = serv->exec();
645  fullExec = exec;
646  exec.replace("%u", "", false);
647  exec.replace("%f", "", false);
648  exec.replace("-caption %c", "");
649  exec.replace("-caption \"%c\"", "");
650  exec.replace("%i", "");
651  exec.replace("%m", "");
652  exec = exec.simplifyWhiteSpace();
653  if (exec == typedExec)
654  {
655  ok = true;
656  m_pService = serv;
657  kdDebug(250) << k_funcinfo << "OK, found identical service: " << serv->desktopEntryPath() << endl;
658  }
659  }
660  if (!ok) // service was found, but it was different -> keep looking
661  {
662  ++i;
663  serviceName = initialServiceName + "-" + TQString::number(i);
664  }
665  }
666  while (!ok);
667  }
668  if ( m_pService )
669  {
670  // Existing service selected
671  serviceName = m_pService->name();
672  initialServiceName = serviceName;
673  fullExec = m_pService->exec();
674  }
675 
676  if (terminal->isChecked())
677  {
678  TDEConfigGroup confGroup( TDEGlobal::config(), TQString::fromLatin1("General") );
679  preferredTerminal = confGroup.readPathEntry("TerminalApplication", TQString::fromLatin1("konsole"));
680  m_command = preferredTerminal;
681  // only add --noclose when we are sure it is konsole we're using
682  if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
683  m_command += TQString::fromLatin1(" --noclose");
684  m_command += TQString::fromLatin1(" -e ");
685  m_command += edit->url();
686  kdDebug(250) << "Setting m_command to " << m_command << endl;
687  }
688  if ( m_pService && terminal->isChecked() != m_pService->terminal() )
689  m_pService = 0L; // It's not exactly this service we're running
690 
691  bool bRemember = remember && remember->isChecked();
692 
693  if ( !bRemember && m_pService)
694  {
695  accept();
696  return;
697  }
698 
699  if (!bRemember && !d->saveNewApps)
700  {
701  // Create temp service
702  m_pService = new KService(initialServiceName, fullExec, TQString::null);
703  if (terminal->isChecked())
704  {
705  m_pService->setTerminal(true);
706  // only add --noclose when we are sure it is konsole we're using
707  if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
708  m_pService->setTerminalOptions("--noclose");
709  }
710  accept();
711  return;
712  }
713 
714  // if we got here, we can't seem to find a service for what they
715  // wanted. The other possibility is that they have asked for the
716  // association to be remembered. Create/update service.
717 
718  TQString newPath;
719  TQString oldPath;
720  TQString menuId;
721  if (m_pService)
722  {
723  oldPath = m_pService->desktopEntryPath();
724  newPath = m_pService->locateLocal();
725  menuId = m_pService->menuId();
726  kdDebug(250) << "Updating exitsing service " << m_pService->desktopEntryPath() << " ( " << newPath << " ) " << endl;
727  }
728  else
729  {
730  newPath = KService::newServicePath(false /* hidden */, serviceName, &menuId);
731  kdDebug(250) << "Creating new service " << serviceName << " ( " << newPath << " ) " << endl;
732  }
733 
734  int maxPreference = 1;
735  if (!qServiceType.isEmpty())
736  {
737  KServiceTypeProfile::OfferList offerList = KServiceTypeProfile::offers( qServiceType );
738  if (!offerList.isEmpty())
739  maxPreference = offerList.first().preference();
740  }
741 
742  KDesktopFile *desktop = 0;
743  if (!oldPath.isEmpty() && (oldPath != newPath))
744  {
745  KDesktopFile orig(oldPath, true);
746  desktop = orig.copyTo(newPath);
747  }
748  else
749  {
750  desktop = new KDesktopFile(newPath);
751  }
752  desktop->writeEntry("Type", TQString::fromLatin1("Application"));
753  desktop->writeEntry("Name", initialServiceName);
754  desktop->writePathEntry("Exec", fullExec);
755  if (terminal->isChecked())
756  {
757  desktop->writeEntry("Terminal", true);
758  // only add --noclose when we are sure it is konsole we're using
759  if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
760  desktop->writeEntry("TerminalOptions", "--noclose");
761  }
762  else
763  {
764  desktop->writeEntry("Terminal", false);
765  }
766  desktop->writeEntry("X-TDE-InitialPreference", maxPreference + 1);
767 
768 
769  if (bRemember || d->saveNewApps)
770  {
771  TQStringList mimeList = desktop->readListEntry("MimeType", ';');
772  if (!qServiceType.isEmpty() && !mimeList.contains(qServiceType))
773  mimeList.append(qServiceType);
774  desktop->writeEntry("MimeType", mimeList, ';');
775 
776  if ( !qServiceType.isEmpty() )
777  {
778  // Also make sure the "auto embed" setting for this mimetype is off
779  KDesktopFile mimeDesktop( locateLocal( "mime", qServiceType + ".desktop" ) );
780  mimeDesktop.writeEntry( "X-TDE-AutoEmbed", false );
781  mimeDesktop.sync();
782  }
783  }
784 
785  // write it all out to the file
786  desktop->sync();
787  delete desktop;
788 
789  KService::rebuildKSycoca(this);
790 
791  m_pService = KService::serviceByMenuId( menuId );
792 
793  Q_ASSERT( m_pService );
794 
795  accept();
796 }
797 
798 TQString KOpenWithDlg::text() const
799 {
800  if (!m_command.isEmpty())
801  return m_command;
802  else
803  return edit->url();
804 }
805 
806 void KOpenWithDlg::hideNoCloseOnExit()
807 {
808  // uncheck the checkbox because the value could be used when "Run in Terminal" is selected
809  nocloseonexit->setChecked( false );
810  nocloseonexit->hide();
811 }
812 
813 void KOpenWithDlg::hideRunInTerminal()
814 {
815  terminal->hide();
816  hideNoCloseOnExit();
817 }
818 
819 void KOpenWithDlg::accept()
820 {
821  KHistoryCombo *combo = static_cast<KHistoryCombo*>( edit->comboBox() );
822  if ( combo ) {
823  combo->addToHistory( edit->url() );
824 
825  TDEConfig *kc = TDEGlobal::config();
826  TDEConfigGroupSaver ks( kc, TQString::fromLatin1("Open-with settings") );
827  kc->writeEntry( TQString::fromLatin1("History"), combo->historyItems() );
828  kc->writeEntry(TQString::fromLatin1("CompletionMode"),
829  combo->completionMode());
830  // don't store the completion-list, as it contains all of KURLCompletion's
831  // executables
832  kc->sync();
833  }
834 
835  TQDialog::accept();
836 }
837 
838 
840 
841 #ifndef KDE_NO_COMPAT
842 bool KFileOpenWithHandler::displayOpenWithDialog( const KURL::List& urls )
843 {
844  KOpenWithDlg l( urls, i18n("Open with:"), TQString::null, 0L );
845  if ( l.exec() )
846  {
847  KService::Ptr service = l.service();
848  if ( !!service )
849  return KRun::run( *service, urls );
850 
851  kdDebug(250) << "No service set, running " << l.text() << endl;
852  return KRun::run( l.text(), urls );
853  }
854  return false;
855 }
856 #endif
857 
858 #include "kopenwith.moc"
859 #include "kopenwith_p.moc"
860 
KFileOpenWithHandler::displayOpenWithDialog
virtual bool displayOpenWithDialog(const KURL::List &urls)
Opens an open-with dialog box for urls.
Definition: kopenwith.cpp:842
KOpenWithDlg
"Open with" dialog box.
Definition: kopenwith.h:47
KOpenWithDlg::KOpenWithDlg
KOpenWithDlg(const KURL::List &urls, TQWidget *parent=0L)
Create a dialog that asks for a application to open a given URL(s) with.
Definition: kopenwith.cpp:321
KOpenWithDlg::~KOpenWithDlg
~KOpenWithDlg()
Destructor.
Definition: kopenwith.cpp:531
KOpenWithDlg::hideRunInTerminal
void hideRunInTerminal()
Hide the "Run in &terminal" Checkbox.
Definition: kopenwith.cpp:813
KOpenWithDlg::init
void init(const TQString &text, const TQString &value)
Create a dialog that asks for a application to open a given URL(s) with.
Definition: kopenwith.cpp:388
KOpenWithDlg::text
TQString text() const
Definition: kopenwith.cpp:798
KOpenWithDlg::setServiceType
void setServiceType(const KURL::List &_urls)
Determine service type from URLs.
Definition: kopenwith.cpp:376
KOpenWithDlg::slotClear
void slotClear()
The slot for clearing the edit widget.
Definition: kopenwith.cpp:539
KOpenWithDlg::service
KService::Ptr service() const
Definition: kopenwith.h:114
KOpenWithDlg::accept
virtual void accept()
Reimplemented from TQDialog::accept() to save history of the combobox.
Definition: kopenwith.cpp:819
KOpenWithDlg::hideNoCloseOnExit
void hideNoCloseOnExit()
Hide the "Do not &close when command exits" Checkbox.
Definition: kopenwith.cpp:806
KOpenWithDlg::setSaveNewApplications
void setSaveNewApplications(bool b)
Set whether a new .desktop file should be created if the user selects an application for which no cor...
Definition: kopenwith.cpp:608
KURLRequester
This class is a widget showing a lineedit and a button, which invokes a filedialog.
Definition: kurlrequester.h:57
KURLRequester::setURL
void setURL(const TQString &url)
Sets the url in the lineedit to url.
Definition: kurlrequester.cpp:235
KURLRequester::comboBox
KComboBox * comboBox() const
Definition: kurlrequester.cpp:374
KURLRequester::url
TQString url() const
Definition: kurlrequester.cpp:268
KURLRequester::button
KPushButton * button() const
Definition: kurlrequester.cpp:398
KURLRequester::lineEdit
KLineEdit * lineEdit() const
Definition: kurlrequester.cpp:369

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdefile by doxygen 1.9.1
This website is maintained by Timothy Pearson.