Changeset View
Changeset View
Standalone View
Standalone View
plugins/openwith/openwithplugin.cpp
Show All 19 Lines | |||||
20 | 20 | | |||
21 | #include "openwithplugin.h" | 21 | #include "openwithplugin.h" | ||
22 | 22 | | |||
23 | #include <QAction> | 23 | #include <QAction> | ||
24 | #include <QApplication> | 24 | #include <QApplication> | ||
25 | #include <QMenu> | 25 | #include <QMenu> | ||
26 | #include <QMimeDatabase> | 26 | #include <QMimeDatabase> | ||
27 | #include <QMimeType> | 27 | #include <QMimeType> | ||
28 | #include <QSignalMapper> | | |||
29 | #include <QVariantList> | 28 | #include <QVariantList> | ||
30 | 29 | | |||
31 | #include <KSharedConfig> | 30 | #include <KSharedConfig> | ||
32 | #include <KConfigGroup> | 31 | #include <KConfigGroup> | ||
33 | #include <KLocalizedString> | 32 | #include <KLocalizedString> | ||
34 | #include <KMessageBox> | 33 | #include <KMessageBox> | ||
35 | #include <KMimeTypeTrader> | 34 | #include <KMimeTypeTrader> | ||
36 | #include <KParts/MainWindow> | 35 | #include <KParts/MainWindow> | ||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Line(s) | 80 | if (defaultForMimeType(mimeType).isEmpty() && mimeType == QLatin1String("inode/directory")) { | |||
83 | return KMimeTypeTrader::self()->preferredService(mimeType); | 82 | return KMimeTypeTrader::self()->preferredService(mimeType); | ||
84 | } else { | 83 | } else { | ||
85 | return true; | 84 | return true; | ||
86 | } | 85 | } | ||
87 | } | 86 | } | ||
88 | } | 87 | } | ||
89 | 88 | | |||
90 | OpenWithPlugin::OpenWithPlugin ( QObject* parent, const QVariantList& ) | 89 | OpenWithPlugin::OpenWithPlugin ( QObject* parent, const QVariantList& ) | ||
91 | : IPlugin ( QStringLiteral("kdevopenwith"), parent ), | 90 | : IPlugin ( QStringLiteral("kdevopenwith"), parent ) | ||
92 | m_actionMap( nullptr ) | | |||
93 | { | 91 | { | ||
94 | } | 92 | } | ||
95 | 93 | | |||
96 | OpenWithPlugin::~OpenWithPlugin() | 94 | OpenWithPlugin::~OpenWithPlugin() | ||
97 | { | 95 | { | ||
98 | } | 96 | } | ||
99 | 97 | | |||
100 | KDevelop::ContextMenuExtension OpenWithPlugin::contextMenuExtension(KDevelop::Context* context, QWidget* parent) | 98 | KDevelop::ContextMenuExtension OpenWithPlugin::contextMenuExtension(KDevelop::Context* context, QWidget* parent) | ||
101 | { | 99 | { | ||
102 | // do not recurse | 100 | // do not recurse | ||
103 | if (context->hasType(KDevelop::Context::OpenWithContext)) { | 101 | if (context->hasType(KDevelop::Context::OpenWithContext)) { | ||
104 | return ContextMenuExtension(); | 102 | return ContextMenuExtension(); | ||
105 | } | 103 | } | ||
106 | 104 | | |||
107 | m_urls.clear(); | 105 | m_urls.clear(); | ||
108 | m_actionMap.reset(); | | |||
109 | m_services.clear(); | 106 | m_services.clear(); | ||
110 | 107 | | |||
111 | FileContext* filectx = dynamic_cast<FileContext*>( context ); | 108 | FileContext* filectx = dynamic_cast<FileContext*>( context ); | ||
112 | ProjectItemContext* projctx = dynamic_cast<ProjectItemContext*>( context ); | 109 | ProjectItemContext* projctx = dynamic_cast<ProjectItemContext*>( context ); | ||
113 | if ( filectx && filectx->urls().count() > 0 ) { | 110 | if ( filectx && filectx->urls().count() > 0 ) { | ||
114 | m_urls = filectx->urls(); | 111 | m_urls = filectx->urls(); | ||
115 | } else if ( projctx && projctx->items().count() > 0 ) { | 112 | } else if ( projctx && projctx->items().count() > 0 ) { | ||
116 | // For now, let's handle *either* files only *or* directories only | 113 | // For now, let's handle *either* files only *or* directories only | ||
117 | const int wantedType = projctx->items().at(0)->type(); | 114 | const int wantedType = projctx->items().at(0)->type(); | ||
118 | foreach( ProjectBaseItem* item, projctx->items() ) { | 115 | foreach( ProjectBaseItem* item, projctx->items() ) { | ||
119 | if (wantedType == ProjectBaseItem::File && item->file()) { | 116 | if (wantedType == ProjectBaseItem::File && item->file()) { | ||
120 | m_urls << item->file()->path().toUrl(); | 117 | m_urls << item->file()->path().toUrl(); | ||
121 | } else if ((wantedType == ProjectBaseItem::Folder || wantedType == ProjectBaseItem::BuildFolder) && item->folder()) { | 118 | } else if ((wantedType == ProjectBaseItem::Folder || wantedType == ProjectBaseItem::BuildFolder) && item->folder()) { | ||
122 | m_urls << item->folder()->path().toUrl(); | 119 | m_urls << item->folder()->path().toUrl(); | ||
123 | } | 120 | } | ||
124 | } | 121 | } | ||
125 | } | 122 | } | ||
126 | 123 | | |||
127 | if (m_urls.isEmpty()) { | 124 | if (m_urls.isEmpty()) { | ||
128 | return KDevelop::ContextMenuExtension(); | 125 | return KDevelop::ContextMenuExtension(); | ||
129 | } | 126 | } | ||
130 | 127 | | |||
131 | m_actionMap.reset(new QSignalMapper( this )); | | |||
132 | connect( m_actionMap.data(), static_cast<void(QSignalMapper::*)(const QString&)>(&QSignalMapper::mapped), this, &OpenWithPlugin::open ); | | |||
133 | | ||||
134 | // Ok, lets fetch the mimetype for the !!first!! url and the relevant services | 128 | // Ok, lets fetch the mimetype for the !!first!! url and the relevant services | ||
135 | // TODO: Think about possible alternatives to using the mimetype of the first url. | 129 | // TODO: Think about possible alternatives to using the mimetype of the first url. | ||
136 | QMimeType mimetype = QMimeDatabase().mimeTypeForUrl(m_urls.first()); | 130 | QMimeType mimetype = QMimeDatabase().mimeTypeForUrl(m_urls.first()); | ||
137 | m_mimeType = mimetype.name(); | 131 | m_mimeType = mimetype.name(); | ||
138 | 132 | | |||
139 | QList<QAction*> partActions = actionsForServiceType(QStringLiteral("KParts/ReadOnlyPart"), parent); | 133 | QList<QAction*> partActions = actionsForServiceType(QStringLiteral("KParts/ReadOnlyPart"), parent); | ||
140 | QList<QAction*> appActions = actionsForServiceType(QStringLiteral("Application"), parent); | 134 | QList<QAction*> appActions = actionsForServiceType(QStringLiteral("Application"), parent); | ||
141 | 135 | | |||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Line(s) | 182 | { | |||
196 | foreach( KService::Ptr svc, list ) { | 190 | foreach( KService::Ptr svc, list ) { | ||
197 | QAction* act = new QAction(isTextEditor(svc) ? i18n("Default Editor") : svc->name(), parent); | 191 | QAction* act = new QAction(isTextEditor(svc) ? i18n("Default Editor") : svc->name(), parent); | ||
198 | act->setIcon( QIcon::fromTheme( svc->icon() ) ); | 192 | act->setIcon( QIcon::fromTheme( svc->icon() ) ); | ||
199 | if (svc->storageId() == defaultId || (defaultId.isEmpty() && isTextEditor(svc))) { | 193 | if (svc->storageId() == defaultId || (defaultId.isEmpty() && isTextEditor(svc))) { | ||
200 | QFont font = act->font(); | 194 | QFont font = act->font(); | ||
201 | font.setBold(true); | 195 | font.setBold(true); | ||
202 | act->setFont(font); | 196 | act->setFont(font); | ||
203 | } | 197 | } | ||
204 | connect(act, &QAction::triggered, m_actionMap.data(), static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map)); | 198 | const QString sid = svc->storageId(); | ||
205 | m_actionMap->setMapping( act, svc->storageId() ); | 199 | connect(act, &QAction::triggered, this, [this, sid]() { open(sid); } ); | ||
206 | actions << act; | 200 | actions << act; | ||
207 | if ( isTextEditor(svc) ) { | 201 | if ( isTextEditor(svc) ) { | ||
208 | standardAction = act; | 202 | standardAction = act; | ||
209 | } else if ( svc->storageId() == pref->storageId() ) { | 203 | } else if ( svc->storageId() == pref->storageId() ) { | ||
210 | standardAction = act; | 204 | standardAction = act; | ||
211 | } | 205 | } | ||
212 | } | 206 | } | ||
213 | std::sort(actions.begin(), actions.end(), sortActions); | 207 | std::sort(actions.begin(), actions.end(), sortActions); | ||
▲ Show 20 Lines • Show All 77 Lines • Show Last 20 Lines |