summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/sms/services/smssendprovider.cpp
blob: 3ca0b4195e7c81e16695e81a7d067bed5c0a9a6f (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*  *************************************************************************
    *   copyright: (C) 2003 Richard Lärkäng <nouseforaname@home.se>         *
    *   copyright: (C) 2003 Gav Wood <gav@kde.org>                          *
    *************************************************************************
*/

/*  *************************************************************************
    *                                                                       *
    * 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 <tqvaluelist.h>
#include <tqlabel.h>
#include <tqfile.h>

#include <tdeconfigbase.h>
#include <kprocess.h>
#include <klineedit.h>
#include <tdemessagebox.h>
#include <kdebug.h>
#include <tdelocale.h>

#include "kopeteaccount.h"
#include "kopeteuiglobal.h"

#include "smssendprovider.h"
#include "smsprotocol.h"
#include "smscontact.h"

SMSSendProvider::SMSSendProvider(const TQString& providerName, const TQString& prefixValue, Kopete::Account* account, TQObject* parent, const char *name)
	: TQObject( parent, name ), m_account(account)
{
	kdWarning( 14160 ) << k_funcinfo << "this = " << this << ", m_account = " << m_account << " (should be ok if zero!!)" << endl;

	provider = providerName;
	prefix = prefixValue;
	m_maxSize = 160;

	messagePos = -1;
	telPos = -1;

	TQString file = prefix + "/share/smssend/" + provider + ".sms";
	TQFile f(file);
	if (f.open(IO_ReadOnly))
	{
		TQTextStream t(&f);
		TQString group = TQString("SMSSend-%1").arg(provider);
		bool exactNumberMatch = false;
		TQStringList numberWords;
		numberWords.append("Tel");
                numberWords.append("Number");
		numberWords.append("number");
		numberWords.append("TelNum");
		numberWords.append("Recipient");
		numberWords.append("Tel1");
		numberWords.append("To");
		numberWords.append("nummer");
		numberWords.append("telefone");
		numberWords.append("ToPhone");

		while( !t.eof())
		{
			TQString s = t.readLine();
			if( s[0] == '%')
			{
				TQStringList args = TQStringList::split(':',s);
				TQStringList options = TQStringList::split(' ', args[0]);

				names.append(options[0].replace(0,1,""));

				bool hidden = false;
				for(unsigned i = 1; i < options.count(); i++)
					if(options[i] == "Hidden")
					{	hidden = true;
						break;
					}
				isHiddens.append(hidden);

				// Strip trailing whitespace in the end
				// and '%' in the beginning
				args[0] = args[0].simplifyWhiteSpace().mid(1);

				descriptions.append(args[1]);
				if (m_account)
					values.append(m_account->configGroup()->readEntry(TQString("%1:%2").arg(group).arg(names[names.count()-1]),
					                                                  TQString()));
				else
					values.append("");

				if( args[0].contains("Message") || args[0].contains("message")
					|| args[0].contains("message") || args[0].contains("nachricht")
					|| args[0].contains("Msg") || args[0].contains("Mensagem") )
				{
					for( unsigned i = 0; i < options.count(); i++)
					{
						if (options[i].contains("Size="))
						{
							TQString option = options[i];
							option.replace(0,5,"");
							m_maxSize = option.toInt();
						}
					}
					messagePos = names.count()-1;
				}
				else if (!exactNumberMatch)
				{
					for (TQStringList::Iterator it=numberWords.begin(); it != numberWords.end(); ++it)
					{
						if (args[0].contains(*it))
						{
							telPos = names.count() - 1;
							if (args[0] == *it)
							{
//								kdDebug(14160) << "Exact match for " << args[0] << endl;
								exactNumberMatch = true;
							}
//							kdDebug(14160) << "args[0] (" << args[0] << ") contains " << *it << endl;
						}
					}
				}
			}
		}
	}
	f.close();

	if ( messagePos == -1 || telPos == -1 )
	{
		canSend = false;
		return;
	}

	canSend = true;
}

SMSSendProvider::~SMSSendProvider()
{
	kdWarning( 14160 ) << k_funcinfo << "this = " << this << endl;
}

void SMSSendProvider::setAccount(Kopete::Account *account)
{
	m_account = account;
}

const TQString& SMSSendProvider::name(int i)
{
	if ( telPos == i || messagePos == i)
		return TQString();
	else
		return names[i];
}

const TQString& SMSSendProvider::value(int i)
{
	return values[i];
}

const TQString& SMSSendProvider::description(int i)
{
	return descriptions[i];
}

const bool SMSSendProvider::isHidden(int i)
{
	return isHiddens[i];
}

void SMSSendProvider::save(TQPtrList<KLineEdit>& args)
{
	kdDebug( 14160 ) << k_funcinfo << "m_account = " << m_account << " (should be non-zero!!)" << endl;
	if (!m_account) return;		// prevent crash in worst case

	TQString group = TQString("SMSSend-%1").arg(provider);
	int namesI=0;

	for (unsigned i=0; i < args.count(); i++)
	{
		if (telPos == namesI || messagePos == namesI)
		{
//		    kdDebug(14160) << k_funcinfo << "Skipping pos " << namesI << endl;
		    namesI++;
		    if (telPos == namesI || messagePos == namesI)
		    {
//		        kdDebug(14160) << k_funcinfo << "Skipping pos " << namesI << endl;
		        namesI++;
		    }
		}

//                kdDebug(14160) << k_funcinfo << "saving " << args.at(i) << " to " << names[namesI] << endl;
		if (!args.at(i)->text().isEmpty())
		{	values[namesI] = args.at(i)->text();
			m_account->configGroup()->writeEntry(TQString("%1:%2").arg(group).arg(names[namesI]), values[namesI]);
		}
	        namesI++;
	}
}

int SMSSendProvider::count()
{
	return names.count();
}

void SMSSendProvider::send(const Kopete::Message& msg)
{
	if ( canSend == false )
	{
		if ( messagePos == -1 )
		{
			canSend = false;
			KMessageBox::error(Kopete::UI::Global::mainWidget(), i18n("Could not determine which argument which should contain the message."),
				i18n("Could Not Send Message"));
			return;
		}
		if ( telPos == -1 )
		{
			canSend = false;

			KMessageBox::error(Kopete::UI::Global::mainWidget(), i18n("Could not determine which argument which should contain the number."),
				i18n("Could Not Send Message"));
			return;
		}
	}

	m_msg = msg;

	TQString message = msg.plainBody();
	TQString nr = dynamic_cast<SMSContact *>(msg.to().first())->qualifiedNumber();

	if (canSend = false)
		return;

	values[messagePos] = message;
	values[telPos] = nr;

	TDEProcess* p = new TDEProcess;

	kdWarning( 14160 ) << "Executing " << TQString("%1/bin/smssend").arg(prefix) << " \"" << provider << "\" " << values.join("\" \"") << "\"" << endl;

	*p << TQString("%1/bin/smssend").arg(prefix) << provider << values;

	output = "";
	connect( p, TQT_SIGNAL(processExited(TDEProcess *)), this, TQT_SLOT(slotSendFinished(TDEProcess *)));
	connect( p, TQT_SIGNAL(receivedStdout(TDEProcess *, char *, int)), this, TQT_SLOT(slotReceivedOutput(TDEProcess *, char *, int)));
//	connect( p, TQT_SIGNAL(receivedStderr(TDEProcess *, char *, int)), this, TQT_SLOT(slotReceivedOutput(TDEProcess *, char *, int)));

	p->start(TDEProcess::NotifyOnExit, TDEProcess::AllOutput);
}

void SMSSendProvider::slotSendFinished(TDEProcess *p)
{
	kdWarning( 14160 ) << k_funcinfo << "this = " << this << ", es = " << p->exitStatus() << ", p = " << p << " (should be non-zero!!)" << endl;
	if (p->exitStatus() == 0)
		emit messageSent(m_msg);
	else
		emit messageNotSent(m_msg, TQString().setLatin1(output));

	p->deleteLater();
}

void SMSSendProvider::slotReceivedOutput(TDEProcess *, char *buffer, int buflen)
{
//	TQStringList lines = TQStringList::split("\n", TQString::fromLocal8Bit(buffer, buflen));
//	for (TQStringList::Iterator it = lines.begin(); it != lines.end(); ++it)
	for(int i = 0; i < buflen; i++)
		output += buffer[i];
	kdWarning( 14160 ) << k_funcinfo << " output now = " << output << endl;
}

int SMSSendProvider::maxSize()
{
	return m_maxSize;
}

#include "smssendprovider.moc"