summaryrefslogtreecommitdiffstats
path: root/kdcop/kdcoplistview.cpp
blob: 07e0b9fb873d34b06904d60d82919bc32c063a95 (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
/*
 * Copyright (C) 2000 by Ian Reinhart Geiser <geiseri@kde.org>
 *
 * Licensed under the Artistic License.
 */

#include "kdcoplistview.h"
#include "kdcoplistview.moc"
#include <kdebug.h>
#include <qdragobject.h>
#include <qstringlist.h>
#include <qregexp.h>


KDCOPListView::KDCOPListView ( QWidget *parent, const char *name)
    : KListView(parent, name)
{
	kdDebug() << "Building new list." << endl;
	setDragEnabled(true);
}


KDCOPListView::~KDCOPListView ()
{

}

QDragObject *KDCOPListView::dragObject()
{
	kdDebug() << "Drag object called... " << endl;
	if(!currentItem())
		return 0;
	else
		return new QTextDrag(encode(this->selectedItem()), this);
}

void KDCOPListView::setMode( const QString &theMode )
{
	mode = theMode;
}

QString KDCOPListView::encode(QListViewItem *theCode)
{
	DCOPBrowserItem * item = static_cast<DCOPBrowserItem *>(theCode);

	if (item->type() != DCOPBrowserItem::Function)
	return "";

	DCOPBrowserFunctionItem * fitem =
	static_cast<DCOPBrowserFunctionItem *>(item);

	QString function = QString::fromUtf8(fitem->function());
	QString application = QString::fromUtf8(fitem->app());
	QString object = QString::fromUtf8(fitem->object());

	kdDebug() << function << endl;
	QString returnType = function.section(' ', 0,0);
	QString returnCode = "";
	QString normalisedSignature;
	QStringList types;
	QStringList names;

	QString unNormalisedSignature(function);

	int s = unNormalisedSignature.find(' ');

	if ( s < 0 )
		s = 0;
	else
		s++;

	unNormalisedSignature = unNormalisedSignature.mid(s);

	int left  = unNormalisedSignature.find('(');
	int right = unNormalisedSignature.findRev(')');

	if (-1 == left)
	{
	// Fucked up function signature.
	return "";
	}
	if (left > 0 && left + 1 < right - 1)
	{
		types = QStringList::split
		(',', unNormalisedSignature.mid(left + 1, right - left - 1));
		for (QStringList::Iterator it = types.begin(); it != types.end(); ++it)
		{
			(*it) = (*it).stripWhiteSpace();
			int s = (*it).find(' ');
			if (-1 != s)
			{
				names.append((*it).mid(s + 1));
				(*it) = (*it).left(s);
			}
		}
	}

	if ( mode == "C++")
	{
		QString args;
		for( unsigned int i = 0; i < names.count(); i++)
		{
			args += types[i] + " " + names[i] + ";\n";
		}
		QString dcopRef = "DCOPRef m_" + application + object
			+ "(\""+ application + "\",\"" + object +"\");\n";

		QString stringNames = names.join(",");
		QString stringTypes = types.join(",");
		if( returnType != "void")
			returnType += " return" + returnType + " =";
		else
			returnType = "";
		returnCode = args
			+ dcopRef
			+ returnType
			+ "m_" + application + object
			+ ".call(\"" + unNormalisedSignature.left(left)
			+ "(" + stringTypes + ")\"";
			if(!stringNames.isEmpty())
				returnCode += ", ";
			returnCode += stringNames + ");\n";
	}
	else if (mode == "Shell")
	{
		returnCode = "dcop " + application + " " + object + " "
			+ unNormalisedSignature.left(left) + " " + names.join(" ");
	}
	else if (mode == "Python")
	{
		QString setup;
		setup = "m_"
			+ application + object
			+ " = dcop.DCOPObject( \""
			+ application + "\",\""
		 	+ object + "\")\n";

		for( unsigned int i = 0; i < names.count(); i++)
		{
			setup += names[i] + " #set value here.\n";
		}
		returnCode = setup
			+ "reply"
			+ returnType
			+ " = m_"
			+ application + object + "."
			+ unNormalisedSignature.left(left)
			+ "(" + names.join(",") + ")\n";
	}
	return returnCode;
}

QString KDCOPListView::getCurrentCode() const
{
	// fixing warning
	return QString::null;
}