Krazy/EBN: qMin is better than QMIN
[kphotoalbum.git] / MainWindow / ExternalPopup.cpp
blobc4802fd9e86a566f0e5db04827c36d08509f4b2f
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"
21 #include <ktrader.h>
22 #include <qstringlist.h>
23 #include <qlabel.h>
24 #include <QPixmap>
25 #include <QFile>
26 #include <kservice.h>
27 #include <kurl.h>
28 #include <krun.h>
29 #include <kshell.h>
30 #include <klocale.h>
31 #include <kfileitem.h>
32 #include <kdialog.h>
33 #include <kdebug.h>
34 #include <KMimeTypeTrader>
35 #include <KIcon>
36 #include "Window.h"
37 #include "RunDialog.h"
39 void MainWindow::ExternalPopup::populate( DB::ImageInfoPtr current, const QStringList& imageList )
41 _list = imageList;
42 _currentInfo = current;
43 clear();
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 )
48 continue;
50 const bool multiple = (_list.count() > 1);
51 const bool enabled = (which != 1 && _currentInfo ) || (which == 1 && multiple);
53 // Title
54 QAction* action = addAction( list[which] );
55 QFont fnt = font();
56 fnt.setPointSize( static_cast<int>(fnt.pointSize()*1.5));
57 fnt.setBold(true);
58 action->setFont( fnt );
59 action->setData( -1 );
60 action->setEnabled( enabled );
62 // Fetch set of offers
63 OfferType offers;
64 if ( which == 0 )
65 offers = appInfos( QStringList() << current->fileName(DB::AbsolutePath) );
66 else
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 );
77 // A personal command
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 );
84 // A personal command
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
100 KUrl::List lst;
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();
113 QRegExp origRegexp =
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 );
122 } else {
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 );
127 } else {
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);
138 dialog->show();
140 return;
143 // check for the special entry for self-defined
144 if (name == i18n("Open With...")) {
145 KRun::displayOpenWithDialog(lst, MainWindow::Window::theMainWindow());
146 return;
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 )
158 :QMenu( 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 )
171 StringSet res;
172 for( QStringList::ConstIterator fileIt = files.begin(); fileIt != files.end(); ++fileIt ) {
173 res.insert( mimeType( *fileIt ) );
175 return res;
178 MainWindow::OfferType MainWindow::ExternalPopup::appInfos(const QStringList& files )
180 StringSet types = mimeTypes( files );
181 OfferType res;
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 );
189 return res;
192 bool operator<( const QPair<QString,QPixmap>& a, const QPair<QString,QPixmap>& b )
194 return a.first < b.first;
198 #include "ExternalPopup.moc"