summaryrefslogtreecommitdiffstats
path: root/parts/openwith/openwithpart.cpp
blob: a223f193cb4eb15d0a33405158732357916ad1a0 (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 <tqfile.h>

#include <tdepopupmenu.h>
#include <tdevgenericfactory.h>
#include <kdebug.h>
#include <kmimetype.h>
#include <ktrader.h>
#include <krun.h>
#include <tdeaction.h>
#include <kcharsets.h>

#include "tdevpartcontroller.h"
#include "tdevcore.h"
#include "tdevplugininfo.h"
#include "urlutil.h"

typedef TDevGenericFactory<OpenWithPart> OpenWithFactory;

static const TDevPluginInfo data("tdevopenwith");
K_EXPORT_COMPONENT_FACTORY(libtdevopenwith, OpenWithFactory(data))

OpenWithPart::OpenWithPart(TQObject *parent, const char *name, const TQStringList &)
  : TDevPlugin(&data, parent, name ? name : "OpenWithPart")
{
  setInstance( OpenWithFactory::instance() );

  connect(core(), TQT_SIGNAL(contextMenu(TQPopupMenu*,const Context *)),
	  this, TQT_SLOT(fillContextMenu(TQPopupMenu*,const Context *)));
}


OpenWithPart::~OpenWithPart()
{
}


void OpenWithPart::fillContextMenu(TQPopupMenu *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, TQT_SLOT(openAsUtf8()) );

	TDEPopupMenu * openAsPopup = new TDEPopupMenu( 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."));

	TQStringList encodings = TDEGlobal::charsets()->descriptiveEncodingNames();

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

	TDEPopupMenu * openWithPopup = new TDEPopupMenu( popup );

	// hmmm.. what to do here? open every different mimetype seperately? Just go with the first for now..
	TQString mimeType = KMimeType::findByURL(m_urls.first(), 0, true, true)->name();
	TDETrader::OfferList offers = TDETrader::self()->query(mimeType, "Type == 'Application'");
	if (offers.count() > 0)
	{
		TDETrader::OfferList::Iterator it;
		for (it = offers.begin(); it != offers.end(); ++it)
		{
			TDEAction *action = new TDEAction((*it)->name(), (*it)->icon(), 0, 0, TQFile::encodeName( (*it)->desktopEntryPath() ).data());
			connect(action, TQT_SIGNAL(activated()), this, TQT_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, TQT_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(TQT_TQOBJECT(const_cast<TQT_BASE_OBJECT_NAME*>(sender()))->name());
  if (ptr)
  {
    KRun::run(*ptr, m_urls);
  }
}


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


void OpenWithPart::openAsEncoding( int id )
{
	TQStringList encodings = TDEGlobal::charsets()->descriptiveEncodingNames();
	TQString encoding;
	if ( id <= encodings.count() && id >= 0 )
	{
		encoding = TDEGlobal::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"