[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [KDevPlatform] d7c95d9: make it possible to always open some
From:       Milian Wolff <mail () milianw ! de>
Date:       2010-11-27 19:42:13
Message-ID: 20101127194213.53779A60AE () git ! kde ! org
[Download RAW message or body]


	A	 plugins/openwith/iopenwith.h	 [License: LGPL(v2)]

commit d7c95d92968747816d39cbbf3336c97a81b578e4
Author: Milian Wolff <mail@milianw.de>
Date:   Sat Nov 27 20:25:22 2010 +0100

    make it possible to always open some mimetypes in an external application
    
    we now ask the user whether he wants to change the default whenever he uses the \
'Open With' action  The project manager view is already adapted to the new IOpenWith

diff --git a/plugins/openwith/CMakeLists.txt b/plugins/openwith/CMakeLists.txt
index 07d850b..d735fbb 100644
--- a/plugins/openwith/CMakeLists.txt
+++ b/plugins/openwith/CMakeLists.txt
@@ -14,3 +14,4 @@ install(TARGETS kdevopenwith DESTINATION ${PLUGIN_INSTALL_DIR})
 
 install(FILES kdevopenwith.desktop DESTINATION ${SERVICES_INSTALL_DIR})
 #install(FILES kdevopenwithui.rc DESTINATION ${DATA_INSTALL_DIR}/kdevopenwith)
+install( FILES iopenwith.h DESTINATION ${INCLUDE_INSTALL_DIR}/kdevelop/openwith \
COMPONENT Devel) \ No newline at end of file
diff --git a/plugins/openwith/iopenwith.h b/plugins/openwith/iopenwith.h
new file mode 100644
index 0000000..06c07c1
--- /dev/null
+++ b/plugins/openwith/iopenwith.h
@@ -0,0 +1,43 @@
+/*
+* This file is part of KDevelop
+* Copyright 2010 Milian Wolff <mail@milianw.de>
+*
+* This program is free software; you can redistribute it and/or modify
+* it under the terms of the GNU Library General Public License as
+* published by the Free Software Foundation; either version 2 of the
+* License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public
+* License along with this program; if not, write to the
+* Free Software Foundation, Inc.,
+* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#ifndef IOPENWITH_H
+#define IOPENWITH_H
+#include <KUrl>
+
+namespace KDevelop {
+
+/**
+ * Interface for the OpenWith plugin.
+ */
+class IOpenWith {
+public:
+    virtual ~IOpenWith() {}
+    /**
+     * Open @p files with the preferred applications or parts as chosen by the user.
+     */
+    virtual void openFiles(const KUrl::List &files) = 0;
+};
+
+}
+
+Q_DECLARE_INTERFACE( KDevelop::IOpenWith, "org.kdevelop.IOpenWith" )
+
+#endif // IOPENWITH_H
diff --git a/plugins/openwith/kdevopenwith.desktop \
b/plugins/openwith/kdevopenwith.desktop index 37fb050..6b6ac38 100644
--- a/plugins/openwith/kdevopenwith.desktop
+++ b/plugins/openwith/kdevopenwith.desktop
@@ -86,5 +86,6 @@ X-KDevelop-Version=11
 X-KDevelop-Category=Global
 X-KDE-PluginInfo-Name=kdevopenwith
 X-KDE-PluginInfo-Author=Andreas Pakulat
+X-KDevelop-Interfaces=org.kdevelop.IOpenWith
 X-KDE-PluginInfo-License=GPL
 X-KDevelop-Mode=GUI
diff --git a/plugins/openwith/openwithplugin.cpp \
b/plugins/openwith/openwithplugin.cpp index 41d6491..99a69f7 100644
--- a/plugins/openwith/openwithplugin.cpp
+++ b/plugins/openwith/openwithplugin.cpp
@@ -41,6 +41,9 @@
 #include <interfaces/iruncontroller.h>
 #include <interfaces/idocumentcontroller.h>
 #include <kparts/mainwindow.h>
+#include <KMessageBox>
+#include <QApplication>
+#include <KConfigGroup>
 
 using namespace KDevelop;
 
@@ -53,6 +56,7 @@ OpenWithPlugin::OpenWithPlugin ( QObject* parent, const \
QVariantList& )  m_actionMap( 0 )
 {
 //    setXMLFile( "kdevopenwithui.rc" );
+    KDEV_USE_EXTENSION_INTERFACE( IOpenWith )
 }
 
 OpenWithPlugin::~OpenWithPlugin()
@@ -90,10 +94,12 @@ KDevelop::ContextMenuExtension \
                OpenWithPlugin::contextMenuExtension ( KDevelop::
         // Ok, lets fetch the mimetype for the !!first!! url and the relevant \
                services
         // TODO: Think about possible alternatives to using the mimetype of the \
first url.  KMimeType::Ptr mimetype = KMimeType::findByUrl( m_urls.first() );
-        KService::List apps = KMimeTypeTrader::self()->query( mimetype->name() );
-        KService::Ptr preferredapp = KMimeTypeTrader::self()->preferredService( \
                mimetype->name() );
-        KService::List parts = KMimeTypeTrader::self()->query( mimetype->name(), \
                "KParts/ReadOnlyPart" );
-        KService::Ptr preferredpart = KMimeTypeTrader::self()->preferredService( \
mimetype->name(), "KParts/ReadOnlyPart" ); +        m_mimeType = mimetype->name();
+        KService::List apps = KMimeTypeTrader::self()->query( m_mimeType );
+        KService::Ptr preferredapp = KMimeTypeTrader::self()->preferredService( \
m_mimeType ); +        KService::List parts = KMimeTypeTrader::self()->query( \
m_mimeType, "KParts/ReadOnlyPart" ); +        KService::Ptr preferredpart = \
KMimeTypeTrader::self()->preferredService( m_mimeType, +                              \
"KParts/ReadOnlyPart" );  
         // Now setup a menu with actions for each part and app
         KMenu* menu = new KMenu( i18n("Open With" ) );
@@ -137,6 +143,14 @@ QList< QAction* > OpenWithPlugin::actionsForServices ( const \
KService::List& lis  
 void OpenWithPlugin::openDefault()
 {
+    KConfigGroup config = KGlobal::config()->group("Open With Defaults");
+    if (config.hasKey(m_mimeType)) {
+        QString storageId = config.readEntry(m_mimeType, QString());
+        if (!storageId.isEmpty() && KService::serviceByStorageId(storageId)) {
+            open(storageId);
+            return;
+        }
+    }
     foreach( const KUrl& u, m_urls ) {
         ICore::self()->documentController()->openDocument( u );
     }
@@ -163,4 +177,30 @@ void OpenWithPlugin::open ( const QString& storageid )
             ICore::self()->documentController()->openDocument( u, prefName );
         }
     }
+
+    KConfigGroup config = KGlobal::config()->group("Open With Defaults");
+    if (storageid != config.readEntry(m_mimeType, QString())) {
+        int setDefault = KMessageBox::questionYesNo(
+            qApp->activeWindow(),
+            i18n("Do you want to open %1 files by default with %2?",
+                 m_mimeType, svc->name() ),
+            i18n("Set as default?"),
+            KStandardGuiItem::yes(), KStandardGuiItem::no(),
+            QString("OpenWith-%1").arg(m_mimeType)
+        );
+        if (setDefault == KMessageBox::Yes) {
+            config.writeEntry(m_mimeType, storageid);
+        }
+    }
+}
+
+void OpenWithPlugin::openFiles( const KUrl::List& files )
+{
+    if (files.isEmpty()) {
+        return;
+    }
+
+    m_urls = files;
+    m_mimeType = KMimeType::findByUrl( m_urls.first() )->name();
+    openDefault();
 }
diff --git a/plugins/openwith/openwithplugin.h b/plugins/openwith/openwithplugin.h
index d676690..f50e8e3 100644
--- a/plugins/openwith/openwithplugin.h
+++ b/plugins/openwith/openwithplugin.h
@@ -24,7 +24,8 @@
 #include <interfaces/iplugin.h>
 #include <QVariantList>
 #include <kservice.h>
-#include <kurl.h>
+
+#include "iopenwith.h"
 
 class QSignalMapper;
 namespace KDevelop
@@ -33,20 +34,23 @@ class ContextMenuExtension;
 class Context;
 }
 
-class OpenWithPlugin : public KDevelop::IPlugin
+class OpenWithPlugin : public KDevelop::IPlugin, public KDevelop::IOpenWith
 {
     Q_OBJECT
+    Q_INTERFACES( KDevelop::IOpenWith )
 public:
     OpenWithPlugin ( QObject* parent, const QVariantList& args  );
     virtual ~OpenWithPlugin();
     virtual KDevelop::ContextMenuExtension contextMenuExtension ( KDevelop::Context* \
context ); +    virtual void openFiles( const KUrl::List& files );
 private slots:
     void open( const QString& );
     void openDefault();
 private:
     QList<QAction*> actionsForServices( const KService::List& list, KService::Ptr \
pref );  QSignalMapper* m_actionMap;
-    QList<KUrl> m_urls;
+    KUrl::List m_urls;
+    QString m_mimeType;
 };
 
 #endif // OPENWITHPLUGIN_H
diff --git a/plugins/projectmanagerview/projectmanagerview.cpp \
b/plugins/projectmanagerview/projectmanagerview.cpp index afa4151..a6a16b6 100644
--- a/plugins/projectmanagerview/projectmanagerview.cpp
+++ b/plugins/projectmanagerview/projectmanagerview.cpp
@@ -43,6 +43,7 @@
 #include <interfaces/iselectioncontroller.h>
 #include <interfaces/context.h>
 #include <interfaces/icore.h>
+#include <interfaces/iplugincontroller.h>
 #include <interfaces/iprojectcontroller.h>
 #include <interfaces/iuicontroller.h>
 #include <interfaces/iruncontroller.h>
@@ -52,6 +53,8 @@
 #include <project/interfaces/iprojectbuilder.h>
 #include <project/projectmodel.h>
 
+#include "../openwith/iopenwith.h"
+
 #include <KParts/MainWindow>
 #include <sublime/mainwindow.h>
 
@@ -228,6 +231,14 @@ void ProjectManagerView::locateCurrentDocument()
 
 void ProjectManagerView::openUrl( const KUrl& url )
 {
+    IPlugin* i = ICore::self()->pluginController()->pluginForExtension( \
"org.kdevelop.IOpenWith" ); +    if (i) {
+        KDevelop::IOpenWith* openWith = i->extension<KDevelop::IOpenWith>();
+        Q_ASSERT(openWith);
+        openWith->openFiles(KUrl::List() << url);
+        return;
+    }
+
     ICore::self()->documentController()->openDocument( url );
 }
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic