summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/sms/services/smsclient.cpp
blob: 3391a8d1662b899b5a3e3cf3287c98ef022ec132 (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
/*  *************************************************************************
    *   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 <tqcombobox.h>
#include <tqlayout.h>

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

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

#include "smsclient.h"
#include "smsclientprefs.h"
#include "smsprotocol.h"

SMSClient::SMSClient(Kopete::Account* account)
	: SMSService(account)
{
	prefWidget = 0L;
}

SMSClient::~SMSClient()
{
}

void SMSClient::setWidgetContainer(TQWidget* parent, TQGridLayout* layout)
{
	kdWarning( 14160 ) << k_funcinfo << "ml: " << layout << ", " << "mp: " << parent << endl;
	m_parent = parent;
	m_layout = layout;
	TQWidget *configWidget = configureWidget(parent);
	layout->addMultiCellWidget(configWidget, 0, 1, 0, 1);
	configWidget->show();
}

void SMSClient::send(const Kopete::Message& msg)
{
	kdWarning( 14160 ) << k_funcinfo << "m_account = " << m_account << " (should be non-zero!!)" << endl;
	if (!m_account) return;

	m_msg = msg;

	TDEConfigGroup* c = m_account->configGroup();
	TQString provider = c->readEntry(TQString("%1:%2").arg("SMSClient").arg("ProviderName"));

	if (provider.isNull())
	{
		KMessageBox::error(Kopete::UI::Global::mainWidget(), i18n("No provider configured"), i18n("Could Not Send Message"));
		return;
	}

	TQString programName = c->readEntry(TQString("%1:%2").arg("SMSClient").arg("ProgramName"));
	if (programName.isNull())
		programName = "/usr/bin/sms_client";

	TDEProcess* p = new TDEProcess;

	TQString message = msg.plainBody();
	TQString nr = msg.to().first()->contactId();

	*p << programName;
	*p << provider + ":" + nr;
	*p << message;

	TQObject::connect(p, TQT_SIGNAL(processExited(TDEProcess *)), this, TQT_SLOT(slotSendFinished(TDEProcess*)));
	TQObject::connect(p, TQT_SIGNAL(receivedStdout(TDEProcess*, char*, int)), this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));
	TQObject::connect(p, TQT_SIGNAL(receivedStderr(TDEProcess*, char*, int)), this, TQT_SLOT(slotReceivedOutput(TDEProcess*, char*, int)));

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

TQWidget* SMSClient::configureWidget(TQWidget* parent)
{
	kdWarning( 14160 ) << k_funcinfo << "m_account = " << m_account << " (should be ok if zero!!)" << endl;

	if (prefWidget == 0L)
		prefWidget = new SMSClientPrefsUI(parent);

	prefWidget->configDir->setMode(KFile::Directory);
	TQString configDir;
	if (m_account)
		configDir = m_account->configGroup()->readEntry(TQString("%1:%2").arg("SMSClient").arg("ConfigDir"));
	if (configDir.isNull())
		configDir = "/etc/sms";
	prefWidget->configDir->setURL(configDir);

	TQString programName;
	if (m_account)
		programName = m_account->configGroup()->readEntry(TQString("%1:%2").arg("SMSClient").arg("ProgramName"));
	if (programName.isNull())
		programName = "/usr/bin/sms_client";
	prefWidget->program->setURL(programName);

	prefWidget->provider->insertStringList(providers());

	if (m_account)
	{
		TQString pName = m_account->configGroup()->readEntry(TQString("%1:%2").arg("SMSClient").arg("ProviderName"));
		for (int i=0; i < prefWidget->provider->count(); i++)
		{
			if (prefWidget->provider->text(i) == pName)
			{
				prefWidget->provider->setCurrentItem(i);
				break;
			}
		}
	}

	return prefWidget;
}

void SMSClient::savePreferences()
{
	kdWarning( 14160 ) << k_funcinfo << "m_account = " << m_account << " (should be work if zero!!)" << endl;

	if (prefWidget != 0L && m_account != 0L)
	{
		TDEConfigGroup* c = m_account->configGroup();

		c->writeEntry(TQString("%1:%2").arg("SMSClient").arg("ProgramName"), prefWidget->program->url());
		c->writeEntry(TQString("%1:%2").arg("SMSClient").arg("ConfigDir"), prefWidget->configDir->url());
		c->writeEntry(TQString("%1:%2").arg("SMSClient").arg("ProviderName"), prefWidget->provider->currentText());
	}
}

TQStringList SMSClient::providers()
{
	TQStringList p;

	TQDir d;
	d.setPath(TQString("%1/services/").arg(prefWidget->configDir->url()));
	p += d.entryList("*", TQDir::Files);

	return p;
}

void SMSClient::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)
		output.append(*it);
}

void SMSClient::slotSendFinished(TDEProcess* p)
{
	if (p->exitStatus() == 0)
		emit messageSent(m_msg);
	else
		emit messageNotSent(m_msg, output.join("\n"));
}

int SMSClient::maxSize()
{
	return 160;
}

const TQString& SMSClient::description()
{
	TQString url = "http://www.smsclient.org";
	m_description = i18n("<qt>SMSClient is a program for sending SMS with the modem. The program can be found on <a href=\"%1\">%1</a></qt>").arg(url).arg(url);
	return m_description;
}

#include "smsclient.moc"
/*
 * Local variables:
 * c-indentation-style: k&r
 * c-basic-offset: 8
 * indent-tabs-mode: t
 * End:
 */
// vim: set noet ts=4 sts=4 sw=4: