openwithpart.cpp
Go to the documentation of this file.00001 #include "openwithpart.h"
00002
00003 #include <qfile.h>
00004
00005 #include <kpopupmenu.h>
00006 #include <kdevgenericfactory.h>
00007 #include <kdebug.h>
00008 #include <kmimetype.h>
00009 #include <ktrader.h>
00010 #include <krun.h>
00011 #include <kaction.h>
00012
00013 #include "kdevpartcontroller.h"
00014 #include "kdevcore.h"
00015
00016
00017 static const KAboutData data("kdevopenwith", I18N_NOOP("Open With"), "1.0");
00018 K_EXPORT_COMPONENT_FACTORY(libkdevopenwith, KDevGenericFactory<OpenWithPart>(&data))
00019
00020 OpenWithPart::OpenWithPart(QObject *parent, const char *name, const QStringList &)
00021 : KDevPlugin("OpenWith", "openwith", parent, name ? name : "OpenWithPart")
00022 {
00023 connect(core(), SIGNAL(contextMenu(QPopupMenu*,const Context *)),
00024 this, SLOT(fillContextMenu(QPopupMenu*,const Context *)));
00025 }
00026
00027
00028 OpenWithPart::~OpenWithPart()
00029 {
00030 }
00031
00032
00033 void OpenWithPart::fillContextMenu(QPopupMenu *popup, const Context *context)
00034 {
00035 if (!context->hasType( Context::FileContext ))
00036 return;
00037
00038 const FileContext *ctx = static_cast<const FileContext*>(context);
00039 if (ctx->isDirectory())
00040 return;
00041
00042 popup->insertSeparator();
00043 m_url = KURL(ctx->fileName());
00044
00045 KPopupMenu *sub = new KPopupMenu(popup);
00046
00047 int id = sub->insertItem(i18n("Open as UTF-8"), this, SLOT(openAsEncoding(int)));
00048 sub->setWhatsThis(id, i18n("<b>Open as UTF-8</b><p>Open this file in KDevelop as UTF-8 encoded text."));
00049
00050 QString mimeType = KMimeType::findByURL(m_url, 0, true, true)->name();
00051 KTrader::OfferList offers = KTrader::self()->query(mimeType, "Type == 'Application'");
00052
00053 if (offers.count() > 0)
00054 {
00055
00056 KTrader::OfferList::Iterator it;
00057 for (it = offers.begin(); it != offers.end(); ++it)
00058 {
00059 KAction *action = new KAction((*it)->name(), 0, 0, QFile::encodeName( (*it)->desktopEntryPath() ).data());
00060 connect(action, SIGNAL(activated()), this, SLOT(openWithService()));
00061 action->plug(sub);
00062 }
00063 sub->insertSeparator();
00064
00065 id = popup->insertItem(i18n("Open With"), sub);
00066 popup->setWhatsThis(id, i18n("<b>Open With</b><p>Lists all applications that can be used to open the selected file."));
00067
00068
00069 popup = sub;
00070 }
00071
00072 id = popup->insertItem(i18n("Open With..."), this, SLOT(openWithDialog()));
00073 popup->setWhatsThis(id, i18n("<b>Open With...</b><p>Provides a dialog to choose the application to open the selected file."));
00074 }
00075
00076
00077 void OpenWithPart::openWithService()
00078 {
00079 KService::Ptr ptr = KService::serviceByDesktopPath(sender()->name());
00080 if (ptr)
00081 {
00082 KURL::List list;
00083 list << m_url;
00084
00085 KRun::run(*ptr, list);
00086 }
00087 }
00088
00089
00090 void OpenWithPart::openWithDialog()
00091 {
00092 KURL::List list;
00093 list << m_url;
00094 KRun::displayOpenWithDialog(list);
00095 }
00096
00097
00098 void OpenWithPart::openAsEncoding(int )
00099 {
00100 partController()->setEncoding("utf8");
00101 partController()->editDocument(m_url);
00102 }
00103
00104 #include "openwithpart.moc"
This file is part of the documentation for KDevelop Version 3.1.2.