Changeset View
Changeset View
Standalone View
Standalone View
plugins/externalscript/externalscriptplugin.cpp
Show First 20 Lines • Show All 43 Lines • ▼ Show 20 Line(s) | |||||
44 | #include <KPluginFactory> | 44 | #include <KPluginFactory> | ||
45 | #include <KProcess> | 45 | #include <KProcess> | ||
46 | #include <KLocalizedString> | 46 | #include <KLocalizedString> | ||
47 | 47 | | |||
48 | #include <QAction> | 48 | #include <QAction> | ||
49 | #include <QStandardItemModel> | 49 | #include <QStandardItemModel> | ||
50 | #include <QDBusConnection> | 50 | #include <QDBusConnection> | ||
51 | #include <QMenu> | 51 | #include <QMenu> | ||
52 | #include <QSet> | ||||
flherne: This isn't needed anymore. | |||||
52 | 53 | | |||
53 | 54 | | |||
54 | K_PLUGIN_FACTORY_WITH_JSON(ExternalScriptFactory, "kdevexternalscript.json", registerPlugin<ExternalScriptPlugin>();) | 55 | K_PLUGIN_FACTORY_WITH_JSON(ExternalScriptFactory, "kdevexternalscript.json", registerPlugin<ExternalScriptPlugin>();) | ||
55 | 56 | | |||
56 | class ExternalScriptViewFactory: public KDevelop::IToolViewFactory | 57 | class ExternalScriptViewFactory: public KDevelop::IToolViewFactory | ||
57 | { | 58 | { | ||
58 | public: | 59 | public: | ||
59 | explicit ExternalScriptViewFactory( ExternalScriptPlugin *plugin ): m_plugin( plugin ) {} | 60 | explicit ExternalScriptViewFactory( ExternalScriptPlugin *plugin ): m_plugin( plugin ) {} | ||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Line(s) | 98 | { | |||
103 | setXMLFile( QStringLiteral("kdevexternalscript.rc") ); | 104 | setXMLFile( QStringLiteral("kdevexternalscript.rc") ); | ||
104 | 105 | | |||
105 | //BEGIN load config | 106 | //BEGIN load config | ||
106 | KConfigGroup config = getConfig(); | 107 | KConfigGroup config = getConfig(); | ||
107 | foreach( const QString& group, config.groupList() ) { | 108 | foreach( const QString& group, config.groupList() ) { | ||
108 | KConfigGroup script = config.group( group ); | 109 | KConfigGroup script = config.group( group ); | ||
109 | if ( script.hasKey( "name" ) && script.hasKey( "command" ) ) { | 110 | if ( script.hasKey( "name" ) && script.hasKey( "command" ) ) { | ||
110 | ExternalScriptItem* item = new ExternalScriptItem; | 111 | ExternalScriptItem* item = new ExternalScriptItem; | ||
112 | item->setKey( script.name() ); | ||||
111 | item->setText( script.readEntry( "name" ) ); | 113 | item->setText( script.readEntry( "name" ) ); | ||
112 | item->setCommand( script.readEntry( "command" )); | 114 | item->setCommand( script.readEntry( "command" )); | ||
113 | item->setInputMode( static_cast<ExternalScriptItem::InputMode>( script.readEntry( "inputMode", 0u ) ) ); | 115 | item->setInputMode( static_cast<ExternalScriptItem::InputMode>( script.readEntry( "inputMode", 0u ) ) ); | ||
114 | item->setOutputMode( static_cast<ExternalScriptItem::OutputMode>( script.readEntry( "outputMode", 0u ) ) ); | 116 | item->setOutputMode( static_cast<ExternalScriptItem::OutputMode>( script.readEntry( "outputMode", 0u ) ) ); | ||
115 | item->setErrorMode( static_cast<ExternalScriptItem::ErrorMode>( script.readEntry( "errorMode", 0u ) ) ); | 117 | item->setErrorMode( static_cast<ExternalScriptItem::ErrorMode>( script.readEntry( "errorMode", 0u ) ) ); | ||
116 | item->setSaveMode( static_cast<ExternalScriptItem::SaveMode>( script.readEntry( "saveMode", 0u ) ) ); | 118 | item->setSaveMode( static_cast<ExternalScriptItem::SaveMode>( script.readEntry( "saveMode", 0u ) ) ); | ||
117 | item->setFilterMode( script.readEntry( "filterMode", 0u )); | 119 | item->setFilterMode( script.readEntry( "filterMode", 0u )); | ||
118 | item->action()->setShortcut( QKeySequence( script.readEntry( "shortcuts" ) ) ); | 120 | item->action()->setShortcut( QKeySequence( script.readEntry( "shortcuts" ) ) ); | ||
119 | item->setShowOutput( script.readEntry( "showOutput", true ) ); | 121 | item->setShowOutput( script.readEntry( "showOutput", true ) ); | ||
120 | m_model->appendRow( item ); | 122 | m_model->appendRow( item ); | ||
121 | } | 123 | } | ||
122 | } | 124 | } | ||
123 | //END load config | 125 | //END load config | ||
124 | 126 | | |||
125 | core()->uiController()->addToolView( i18n( "External Scripts" ), m_factory ); | 127 | core()->uiController()->addToolView( i18n( "External Scripts" ), m_factory ); | ||
126 | 128 | | |||
127 | connect( m_model, &QStandardItemModel::rowsRemoved, | 129 | connect( m_model, &QStandardItemModel::rowsAboutToBeRemoved, | ||
128 | this, &ExternalScriptPlugin::rowsRemoved ); | 130 | this, &ExternalScriptPlugin::rowsAboutToBeRemoved ); | ||
129 | connect( m_model, &QStandardItemModel::rowsInserted, | 131 | connect( m_model, &QStandardItemModel::rowsInserted, | ||
130 | this, &ExternalScriptPlugin::rowsInserted ); | 132 | this, &ExternalScriptPlugin::rowsInserted ); | ||
131 | 133 | | |||
132 | const bool firstUse = config.readEntry( "firstUse", true ); | 134 | const bool firstUse = config.readEntry( "firstUse", true ); | ||
133 | if ( firstUse ) { | 135 | if ( firstUse ) { | ||
134 | // some example scripts | 136 | // some example scripts | ||
135 | ExternalScriptItem* item = new ExternalScriptItem; | 137 | ExternalScriptItem* item = new ExternalScriptItem; | ||
136 | item->setText( i18n("Quick Compile") ); | 138 | item->setText( i18n("Quick Compile") ); | ||
▲ Show 20 Lines • Show All 193 Lines • ▼ Show 20 Line(s) | 325 | { | |||
330 | foreach( const QUrl& url, m_urls) { | 332 | foreach( const QUrl& url, m_urls) { | ||
331 | KDevelop::ICore::self()->documentController()->openDocument( url ); | 333 | KDevelop::ICore::self()->documentController()->openDocument( url ); | ||
332 | execute( item, url ); | 334 | execute( item, url ); | ||
333 | } | 335 | } | ||
334 | } | 336 | } | ||
335 | 337 | | |||
336 | void ExternalScriptPlugin::rowsInserted( const QModelIndex& /*parent*/, int start, int end ) | 338 | void ExternalScriptPlugin::rowsInserted( const QModelIndex& /*parent*/, int start, int end ) | ||
337 | { | 339 | { | ||
338 | for ( int i = start; i <= end; ++i ) { | 340 | setupKeys( start, end ); | ||
339 | saveItemForRow( i ); | 341 | for ( int row = start; row <= end; ++row ) { | ||
342 | saveItemForRow( row ); | ||||
340 | } | 343 | } | ||
341 | } | 344 | } | ||
342 | 345 | | |||
343 | void ExternalScriptPlugin::rowsRemoved( const QModelIndex& /*parent*/, int start, int end ) | 346 | void ExternalScriptPlugin::rowsAboutToBeRemoved( const QModelIndex& /*parent*/, int start, int end ) | ||
344 | { | 347 | { | ||
345 | KConfigGroup config = getConfig(); | 348 | KConfigGroup config = getConfig(); | ||
346 | for ( int i = start; i <= end; ++i ) { | 349 | for ( int row = start; row <= end; ++row ) { | ||
347 | KConfigGroup child = config.group( QStringLiteral("script %1").arg(i) ); | 350 | const ExternalScriptItem* const item = static_cast<ExternalScriptItem*>( m_model->item( row ) ); | ||
351 | KConfigGroup child = config.group( item->key() ); | ||||
348 | qCDebug(PLUGIN_EXTERNALSCRIPT) << "removing config group:" << child.name(); | 352 | qCDebug(PLUGIN_EXTERNALSCRIPT) << "removing config group:" << child.name(); | ||
349 | child.deleteGroup(); | 353 | child.deleteGroup(); | ||
350 | } | 354 | } | ||
351 | config.sync(); | 355 | config.sync(); | ||
352 | } | 356 | } | ||
353 | 357 | | |||
354 | void ExternalScriptPlugin::saveItem( const ExternalScriptItem* item ) | 358 | void ExternalScriptPlugin::saveItem( const ExternalScriptItem* item ) | ||
355 | { | 359 | { | ||
356 | const QModelIndex index = m_model->indexFromItem( item ); | 360 | const QModelIndex index = m_model->indexFromItem( item ); | ||
357 | Q_ASSERT( index.isValid() ); | 361 | Q_ASSERT( index.isValid() ); | ||
358 | saveItemForRow( index.row() ); | 362 | | ||
363 | getConfig().group( item->key() ).deleteGroup(); // delete the previous group | ||||
364 | setupKeys( index.row(), index.row() ); | ||||
365 | saveItemForRow( index.row() ); // save the new group | ||||
359 | } | 366 | } | ||
360 | 367 | | |||
361 | void ExternalScriptPlugin::saveItemForRow( int row ) | 368 | void ExternalScriptPlugin::saveItemForRow( int row ) | ||
362 | { | 369 | { | ||
363 | const QModelIndex idx = m_model->index( row, 0 ); | 370 | const QModelIndex idx = m_model->index( row, 0 ); | ||
364 | Q_ASSERT( idx.isValid() ); | 371 | Q_ASSERT( idx.isValid() ); | ||
365 | 372 | | |||
366 | ExternalScriptItem* item = dynamic_cast<ExternalScriptItem*>( m_model->item( row ) ); | 373 | ExternalScriptItem* item = dynamic_cast<ExternalScriptItem*>( m_model->item( row ) ); | ||
367 | Q_ASSERT( item ); | 374 | Q_ASSERT( item ); | ||
368 | 375 | | |||
369 | qCDebug(PLUGIN_EXTERNALSCRIPT) << "save extern script:" << item << idx; | 376 | qCDebug(PLUGIN_EXTERNALSCRIPT) << "save extern script:" << item << idx; | ||
370 | KConfigGroup config = getConfig().group( QStringLiteral("script %1").arg( row ) ); | 377 | KConfigGroup config = getConfig().group( item->key() ); | ||
371 | config.writeEntry( "name", item->text() ); | 378 | config.writeEntry( "name", item->text() ); | ||
372 | config.writeEntry( "command", item->command() ); | 379 | config.writeEntry( "command", item->command() ); | ||
373 | config.writeEntry( "inputMode", (uint) item->inputMode() ); | 380 | config.writeEntry( "inputMode", (uint) item->inputMode() ); | ||
374 | config.writeEntry( "outputMode", (uint) item->outputMode() ); | 381 | config.writeEntry( "outputMode", (uint) item->outputMode() ); | ||
375 | config.writeEntry( "errorMode", (uint) item->errorMode() ); | 382 | config.writeEntry( "errorMode", (uint) item->errorMode() ); | ||
376 | config.writeEntry( "saveMode", (uint) item->saveMode() ); | 383 | config.writeEntry( "saveMode", (uint) item->saveMode() ); | ||
377 | config.writeEntry( "shortcuts", item->action()->shortcut().toString() ); | 384 | config.writeEntry( "shortcuts", item->action()->shortcut().toString() ); | ||
378 | config.writeEntry( "showOutput", item->showOutput() ); | 385 | config.writeEntry( "showOutput", item->showOutput() ); | ||
379 | config.writeEntry( "filterMode", item->filterMode()); | 386 | config.writeEntry( "filterMode", item->filterMode()); | ||
380 | config.sync(); | 387 | config.sync(); | ||
381 | } | 388 | } | ||
382 | 389 | | |||
390 | void ExternalScriptPlugin::setupKeys(int start, int end) | ||||
391 | { | ||||
392 | QStringList keys = getConfig().groupList(); | ||||
Would this better be a member ? amhndu: Would this better be a member ?
Will save the time required to create a new set each call. | |||||
We don't need this at all, it's effectively a reimplementation of KConfigGroup::groupList(). In principle yours is more efficient for this purpose (doesn't needlessly convert the set to a list), but the number of external-scripts will never be large enough for that to matter. flherne: We don't need this at all, it's effectively a reimplementation of [[ https://api.kde. | |||||
amhndu: Thanks, wasn't aware of that function. | |||||
393 | | ||||
394 | for ( int row = start; row <= end; ++row ) { | ||||
395 | ExternalScriptItem* const item = static_cast<ExternalScriptItem*>( m_model->item( row ) ); | ||||
396 | | ||||
397 | int nextSuffix = 2; | ||||
398 | QString keyCandidate = item->text(); | ||||
399 | for (; keys.contains( keyCandidate ); ++nextSuffix) { | ||||
400 | keyCandidate = item->text() + QString::number( nextSuffix ); | ||||
401 | } | ||||
402 | | ||||
403 | qCDebug(PLUGIN_EXTERNALSCRIPT) << "set key" << keyCandidate << "for" << item << item->command(); | ||||
404 | item->setKey(keyCandidate); | ||||
405 | keys.push_back(keyCandidate); | ||||
406 | } | ||||
I'd prefer if this line was just keyCandidate = item->text(), and have maxSuffix initialised to 2. Scripts will almost always have unique names, because users don't want to confuse themselves, and it would be tidier not to have a suffix for the normal case.
rather than
flherne: I'd prefer if this line was just `keyCandidate = item->text()`, and have `maxSuffix`… | |||||
407 | } | ||||
408 | | ||||
383 | #include "externalscriptplugin.moc" | 409 | #include "externalscriptplugin.moc" | ||
384 | 410 | | |||
385 | // kate: indent-mode cstyle; space-indent on; indent-width 2; replace-tabs on; | 411 | // kate: indent-mode cstyle; space-indent on; indent-width 2; replace-tabs on; |
This isn't needed anymore.