1 /* Copyright (C) 2003-2006 Jesper K. Pedersen <blackie@kde.org>
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "ExternalPopup.h"
20 #include "DB/ImageInfo.h"
22 #include <qstringlist.h>
31 #include <kfileitem.h>
34 #include <KMimeTypeTrader>
37 #include "RunDialog.h"
39 void MainWindow::ExternalPopup::populate( DB::ImageInfoPtr current
, const QStringList
& imageList
)
42 _currentInfo
= current
;
45 QStringList list
= QStringList() << i18n("Current Item") << i18n("All Selected Items") << i18n("Copy and Open");
46 for ( int which
= 0; which
< 3; ++which
) {
47 if ( which
== 0 && !current
)
50 const bool multiple
= (_list
.count() > 1);
51 const bool enabled
= (which
!= 1 && _currentInfo
) || (which
== 1 && multiple
);
54 QAction
* action
= addAction( list
[which
] );
56 fnt
.setPointSize( static_cast<int>(fnt
.pointSize()*1.5));
58 action
->setFont( fnt
);
59 action
->setData( -1 );
60 action
->setEnabled( enabled
);
62 // Fetch set of offers
65 offers
= appInfos( QStringList() << current
->fileName(DB::AbsolutePath
) );
67 offers
= appInfos( imageList
);
69 for ( OfferType::const_iterator offerIt
= offers
.begin(); offerIt
!= offers
.end(); ++offerIt
) {
70 QAction
* action
= addAction( (*offerIt
).first
);
71 action
->setObjectName( (*offerIt
).first
); // Notice this is needed to find the application later!
72 action
->setIcon( KIcon((*offerIt
).second
) );
73 action
->setData( which
);
74 action
->setEnabled( enabled
);
78 action
= addAction( i18n("Open With...") );
79 action
->setObjectName( i18n("Open With...") ); // Notice this is needed to find the application later!
80 // XXX: action->setIcon( KIcon((*offerIt).second) );
81 action
->setData( which
);
82 action
->setEnabled( enabled
);
85 // XXX: see kdialog.h for simple usage
86 action
= addAction( i18n("Your Command Line") );
87 action
->setObjectName( i18n("Your Command Line") ); // Notice this is needed to find the application later!
88 // XXX: action->setIcon( KIcon((*offerIt).second) );
89 action
->setData( which
);
90 action
->setEnabled( enabled
);
94 void MainWindow::ExternalPopup::slotExecuteService( QAction
* action
)
96 QString name
= action
->objectName();
97 const StringSet apps
=_appToMimeTypeMap
[name
];
99 // get the list of arguments
102 if ( action
->data() == 1 ) {
103 for( QStringList::Iterator it
= _list
.begin(); it
!= _list
.end(); ++it
) {
104 if ( _appToMimeTypeMap
[name
].contains( mimeType(*it
) ) )
105 lst
.append( KUrl(*it
) );
107 } else if (action
->data() == 2) {
108 QString origFile
= _currentInfo
->fileName(DB::AbsolutePath
);
109 QString newFile
= origFile
;
111 QString origRegexpString
=
112 Settings::SettingsData::instance()->copyFileComponent();
114 QRegExp(origRegexpString
);
115 QString copyFileReplacement
=
116 Settings::SettingsData::instance()->copyFileReplacementComponent();
118 if (origRegexpString
.length() > 0) {
119 newFile
.replace(origRegexp
, copyFileReplacement
);
120 QFile::copy(origFile
, newFile
);
121 lst
.append( newFile
);
123 qWarning("No settings were appropriate for modifying the file name (you must fill in the regexp field; Opening the original instead");
124 lst
.append( origFile
);
128 lst
.append( KUrl(_currentInfo
->fileName(DB::AbsolutePath
)));
132 // get the program to run
134 // check for the special entry for self-defined
135 if (name
== i18n("Your Command Line")) {
137 static RunDialog
* dialog
= new RunDialog(MainWindow::Window::theMainWindow(), _list
);
143 // check for the special entry for self-defined
144 if (name
== i18n("Open With...")) {
145 KRun::displayOpenWithDialog(lst
, MainWindow::Window::theMainWindow());
150 KService::List offers
= KMimeTypeTrader::self()->query( *(apps
.begin()), QString::fromLatin1("Application"),
151 QString::fromLatin1("Name == '%1'").arg(name
));
152 Q_ASSERT( offers
.count() >= 1 );
153 KService::Ptr ptr
= offers
.first();
154 KRun::run(*ptr
, lst
, MainWindow::Window::theMainWindow() );
157 MainWindow::ExternalPopup::ExternalPopup( QWidget
* parent
)
160 setTitle( i18n("Invoke External Program") );
161 connect( this, SIGNAL( triggered( QAction
* ) ), this, SLOT( slotExecuteService( QAction
* ) ) );
164 QString
MainWindow::ExternalPopup::mimeType( const QString
& file
)
166 return KFileItem( KFileItem::Unknown
, KFileItem::Unknown
, KUrl(file
) ).mimetype();
169 Utilities::StringSet
MainWindow::ExternalPopup::mimeTypes( const QStringList
& files
)
172 for( QStringList::ConstIterator fileIt
= files
.begin(); fileIt
!= files
.end(); ++fileIt
) {
173 res
.insert( mimeType( *fileIt
) );
178 MainWindow::OfferType
MainWindow::ExternalPopup::appInfos(const QStringList
& files
)
180 StringSet types
= mimeTypes( files
);
182 for ( StringSet::const_iterator mimeTypeIt
= types
.begin(); mimeTypeIt
!= types
.end(); ++mimeTypeIt
) {
183 KService::List offers
= KMimeTypeTrader::self()->query( *mimeTypeIt
, QLatin1String( "Application" ));
184 for(KService::List::Iterator offerIt
= offers
.begin(); offerIt
!= offers
.end(); ++offerIt
) {
185 res
.insert( qMakePair( (*offerIt
)->name(), (*offerIt
)->icon() ) );
186 _appToMimeTypeMap
[(*offerIt
)->name()].insert( *mimeTypeIt
);
192 bool operator<( const QPair
<QString
,QPixmap
>& a
, const QPair
<QString
,QPixmap
>& b
)
194 return a
.first
< b
.first
;
198 #include "ExternalPopup.moc"