Warning, file /sdk/cervisia/editwithmenu.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright (c) 2004 Christian Loose <christian.loose@kdemail.net>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 
0019 #include "editwithmenu.h"
0020 #include "debug.h"
0021 using namespace Cervisia;
0022 
0023 #include <QMenu>
0024 
0025 #include <KLocalizedString>
0026 #include <KMimeTypeTrader>
0027 #include <QMimeDatabase>
0028 #include <QMimeType>
0029 #include <krun.h>
0030 
0031 EditWithMenu::EditWithMenu(const QUrl &url, QWidget *parent)
0032     : QObject(parent)
0033     , m_menu(0)
0034     , m_url(url)
0035 {
0036     QMimeDatabase db;
0037     QMimeType type = db.mimeTypeForFile(url.path(), QMimeDatabase::MatchExtension);
0038     if (!type.isValid()) {
0039         qCDebug(log_cervisia) << "Couldn't find mime type!";
0040         return;
0041     }
0042 
0043     m_offers = KMimeTypeTrader::self()->query(type.name());
0044 
0045     if (!m_offers.isEmpty()) {
0046         m_menu = new QMenu(i18n("Edit With"));
0047 
0048         KService::List::ConstIterator it = m_offers.constBegin();
0049         for (int i = 0; it != m_offers.constEnd(); ++it, ++i) {
0050             QAction *pAction = m_menu->addAction(QIcon::fromTheme((*it)->icon()), (*it)->name());
0051             pAction->setData(i);
0052         }
0053 
0054         connect(m_menu, SIGNAL(triggered(QAction *)), this, SLOT(actionTriggered(QAction *)));
0055     }
0056 }
0057 
0058 QMenu *EditWithMenu::menu()
0059 {
0060     return m_menu;
0061 }
0062 
0063 void EditWithMenu::actionTriggered(QAction *action)
0064 {
0065     const KService::Ptr service = m_offers[action->data().toInt()];
0066 
0067     QList<QUrl> list;
0068     list.append(m_url);
0069 
0070     KRun::runService(*service, list, 0);
0071 }