summaryrefslogtreecommitdiffstats
path: root/src/pluginselectdialog.cpp
blob: de4a9e1e9e74d067337aa5e4bc01d61fd5bb0836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/***************************************************************************
 *   Copyright (C) 2006 by Jens Dagerbo                                    *
 *   jens.dagerbo@swipnet.se                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <qlistview.h>
#include <qheader.h>
#include <qlabel.h>
#include <qregexp.h>

#include <kdebug.h>
#include <kapplication.h>
#include <kurllabel.h>

#include "kdevplugin.h"
#include "projectmanager.h"
#include "plugincontroller.h"
#include "pluginselectdialog.h"

class PluginItem : public QCheckListItem
{
public:
	// name - "Name", label - "GenericName", info - "Comment"
	PluginItem( QListView * parent, QString const & name, QString const & label,
				QString const & info, QString const url = QString::null )
		: QCheckListItem( parent, label, QCheckListItem::CheckBox),
		_name( name ), _info( info ), _url( url )
	{}

	QString info() { return _info; }
	QString name() { return _name; }
	QString url()  { return _url; }

private:
	QString _name;
	QString _info;
	QString _url;
};


PluginSelectDialog::PluginSelectDialog(QWidget* parent, const char* name, bool modal, WFlags fl )
	: PluginSelectDialogBase( parent,name, modal,fl )
{
	plugin_list->setResizeMode( QListView::LastColumn );
	plugin_list->addColumn("");
	plugin_list->header()->hide();

	connect( plugin_list, SIGNAL( selectionChanged( QListViewItem * ) ), this, SLOT( itemSelected( QListViewItem * ) ) );
	connect( urllabel, SIGNAL( leftClickedURL( const QString & ) ), this, SLOT( openURL( const QString & ) ) );

	init();
}

PluginSelectDialog::~PluginSelectDialog()
{
}

void PluginSelectDialog::saveAsDefault()
{
	kdDebug(9000) << k_funcinfo << endl;

	ProfileEngine & engine = PluginController::getInstance()->engine();
	Profile * profile = engine.findProfile( PluginController::getInstance()->currentProfile() );

	profile->clearList( Profile::ExplicitDisable );

	QListViewItemIterator it( plugin_list );
	while ( it.current() )
	{
		PluginItem * item = static_cast<PluginItem*>( it.current() );
		if ( !item->isOn() )
		{
			profile->addEntry( Profile::ExplicitDisable, item->name() );
		}
		++it;
	}

	profile->save();
}

void PluginSelectDialog::openURL( const QString & url )
{
	kapp->invokeBrowser( url );
}

void PluginSelectDialog::itemSelected( QListViewItem * item )
{
    if ( ! item ) return;

    PluginItem * pitem = static_cast<PluginItem*>( item );
    plugin_description_label->setText( pitem->info() );

	if ( pitem->url().isEmpty() )
	{
		urllabel->clear();
	}
	else
	{
		urllabel->setURL( pitem->url() );
		urllabel->setText( pitem->url() );
	}
}

void PluginSelectDialog::init( )
{
	const QValueList<KDevPlugin*> loadedPlugins = PluginController::getInstance()->loadedPlugins();
	QStringList loadedPluginDesktopNames;
	QValueList<KDevPlugin*>::ConstIterator it = loadedPlugins.begin();
	while( it != loadedPlugins.end() )
	{
		loadedPluginDesktopNames << (*it)->instance()->instanceName();
		++it;
	}

	kdDebug(9000) << " *** loadedPluginDesktopNames: " << loadedPluginDesktopNames << endl;

	KTrader::OfferList localOffers;
	if ( ProjectManager::getInstance()->projectLoaded() )
	{
		localOffers = PluginController::getInstance()->engine().offers(
			PluginController::getInstance()->currentProfile(), ProfileEngine::Project );
	}

	KTrader::OfferList globalOffers = PluginController::getInstance()->engine().offers(
		PluginController::getInstance()->currentProfile(), ProfileEngine::Global);

	KTrader::OfferList offers = localOffers + globalOffers;
	for (KTrader::OfferList::ConstIterator it = offers.begin(); it != offers.end(); ++it)
	{
		// parse out any existing url to make it clickable
		QString Comment = (*it)->comment();
		QRegExp re("\\bhttp://[\\S]*");
		re.search( Comment );
		Comment.replace( re, "" );

		QString url;
		if ( re.pos() > -1 )
		{
			url = re.cap();
		}

		PluginItem *item = new PluginItem( plugin_list, (*it)->desktopEntryName(), (*it)->genericName(), Comment, url );
		item->setOn( loadedPluginDesktopNames.contains( (*it)->desktopEntryName() ) );

		kdDebug(9000) << (*it)->desktopEntryName() << " : " << (loadedPluginDesktopNames.contains( (*it)->desktopEntryName() ) ? "YES" : "NO" ) << endl;
	}

	QListViewItem * first = plugin_list->firstChild();
	if ( first )
	{
		plugin_list->setSelected( first, true );
	}
}

QStringList PluginSelectDialog::unselectedPluginNames( )
{
	QStringList unselectedPlugins;
	QListViewItem * item = plugin_list->firstChild();
	while ( item )
	{
		PluginItem * pluginItem = static_cast<PluginItem*>( item );
		if ( !pluginItem->isOn() )
		{
			unselectedPlugins << pluginItem->name();
		}
		item = item->nextSibling();
	}
	return unselectedPlugins;
}



#include "pluginselectdialog.moc"

// kate: space-indent off; indent-width 4; tab-width 4; show-tabs off;