summaryrefslogtreecommitdiffstats
path: root/parts/openwith/openwithpart.cpp
blob: 4ff33ffbd914f58379d25d607daa901a38c31c72 (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
#include "openwithpart.h"

#include <qfile.h>

#include <kpopupmenu.h>
#include <kdevgenericfactory.h>
#include <kdebug.h>
#include <kmimetype.h>
#include <ktrader.h>
#include <krun.h>
#include <kaction.h>
#include <kcharsets.h>

#include "kdevpartcontroller.h"
#include "kdevcore.h"
#include "kdevplugininfo.h"
#include "urlutil.h"

typedef KDevGenericFactory<OpenWithPart> OpenWithFactory;

static const KDevPluginInfo data("kdevopenwith");
K_EXPORT_COMPONENT_FACTORY(libkdevopenwith, OpenWithFactory(data))

OpenWithPart::OpenWithPart(QObject *parent, const char *name, const QStringList &)
  : KDevPlugin(&data, parent, name ? name : "OpenWithPart")
{
  setInstance( OpenWithFactory::instance() );

  connect(core(), SIGNAL(contextMenu(QPopupMenu*,const Context *)),
	  this, SLOT(fillContextMenu(QPopupMenu*,const Context *)));
}


OpenWithPart::~OpenWithPart()
{
}


void OpenWithPart::fillContextMenu(QPopupMenu *popup, const Context *context)
{
	if ( !context->hasType( Context::FileContext ) ) return;

	const FileContext *ctx = static_cast<const FileContext*>(context);
	m_urls = ctx->urls();

	popup->insertSeparator();

	popup->insertItem( i18n("Open as UTF-8"), this, SLOT(openAsUtf8()) );

	KPopupMenu * openAsPopup = new KPopupMenu( popup );

	int id = popup->insertItem( i18n("Open As"), openAsPopup );
	popup->setWhatsThis(id, i18n("<b>Open As</b><p>Lists all encodings that can be used to open the selected file."));

	QStringList encodings = KGlobal::charsets()->descriptiveEncodingNames();

	int i = 0;
	QStringList::const_iterator it = encodings.constBegin();
	while ( it != encodings.constEnd() )
	{
		openAsPopup->insertItem( *it, this, SLOT(openAsEncoding(int)), 0, i );
		++it;
		i++;
	}

	KPopupMenu * openWithPopup = new KPopupMenu( popup );

	// hmmm.. what to do here? open every different mimetype seperately? Just go with the first for now..
	QString mimeType = KMimeType::findByURL(m_urls.first(), 0, true, true)->name();
	KTrader::OfferList offers = KTrader::self()->query(mimeType, "Type == 'Application'");
	if (offers.count() > 0)
	{
		KTrader::OfferList::Iterator it;
		for (it = offers.begin(); it != offers.end(); ++it)
		{
			KAction *action = new KAction((*it)->name(), (*it)->icon(), 0, 0, QFile::encodeName( (*it)->desktopEntryPath() ).data());
			connect(action, SIGNAL(activated()), this, SLOT(openWithService()));
			action->plug( openWithPopup );
		}
	}

	id = popup->insertItem( i18n("Open With"), openWithPopup );
	popup->setWhatsThis(id, i18n("<b>Open With</b><p>Lists all applications that can be used to open the selected file."));

	openWithPopup->insertSeparator();

	// make sure the generic "Open with ..." entry gets appended to the submenu
	id = openWithPopup->insertItem(i18n("Open With..."), this, SLOT(openWithDialog()));
	openWithPopup->setWhatsThis(id, i18n("<b>Open With...</b><p>Provides a dialog to choose the application to open the selected file."));
}


void OpenWithPart::openWithService()
{
  KService::Ptr ptr = KService::serviceByDesktopPath(sender()->name());
  if (ptr)
  {
    KRun::run(*ptr, m_urls);
  }
}


void OpenWithPart::openWithDialog()
{
  KRun::displayOpenWithDialog( m_urls );
}


void OpenWithPart::openAsEncoding( int id )
{
	QStringList encodings = KGlobal::charsets()->descriptiveEncodingNames();
	QString encoding;
	if ( id <= encodings.count() && id >= 0 )
	{
		encoding = KGlobal::charsets()->encodingForName( encodings[ id ] );
	}

	KURL::List::iterator it = m_urls.begin();
	while ( it != m_urls.end() )
	{
		partController()->setEncoding( encoding );
		partController()->editDocument( *it );
		++it;
	}
}

void OpenWithPart::openAsUtf8( )
{
	KURL::List::iterator it = m_urls.begin();
	while ( it != m_urls.end() )
	{
		partController()->setEncoding( "utf8" );
		partController()->editDocument( *it );
		++it;
	}
}

#include "openwithpart.moc"