~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/sms/services/smssendprovider.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  smssendprovider.cpp  -  SMS Plugin
 
3
 
 
4
  Copyright (c) 2003      by Richard LƤrkƤng        <nouseforaname@home.se>
 
5
  Copyright (c) 2003      by Gav Wood               <gav@kde.org>
 
6
 
 
7
  *************************************************************************
 
8
  *                                                                       *
 
9
  * This program is free software; you can redistribute it and/or modify  *
 
10
  * it under the terms of the GNU General Public License as published by  *
 
11
  * the Free Software Foundation; either version 2 of the License, or     *
 
12
  * (at your option) any later version.                                   *
 
13
  *                                                                       *
 
14
  *************************************************************************
 
15
*/
 
16
 
 
17
#include "smssendprovider.h"
 
18
 
 
19
#include <QFile>
 
20
#include <QTextStream>
 
21
#include <QList>
 
22
 
 
23
#include <kconfigbase.h>
 
24
#include <k3process.h>
 
25
#include <klineedit.h>
 
26
#include <kmessagebox.h>
 
27
#include <kdebug.h>
 
28
#include <klocale.h>
 
29
 
 
30
#include "kopeteaccount.h"
 
31
#include "kopeteuiglobal.h"
 
32
 
 
33
#include "smsprotocol.h"
 
34
#include "smscontact.h"
 
35
 
 
36
SMSSendProvider::SMSSendProvider(const QString& providerName, const QString& prefixValue, Kopete::Account* account, QObject* parent)
 
37
        : QObject( parent ), m_account(account)
 
38
{
 
39
        kWarning( 14160 ) << "this = " << this << ", m_account = " << m_account << " (should be ok if zero!!)";
 
40
 
 
41
        provider = providerName;
 
42
        prefix = prefixValue;
 
43
        m_maxSize = 160;
 
44
 
 
45
        messagePos = -1;
 
46
        telPos = -1;
 
47
 
 
48
        QString file = prefix + "/share/smssend/" + provider + ".sms";
 
49
        QFile f(file);
 
50
        if (f.open(QIODevice::ReadOnly))
 
51
        {
 
52
                QTextStream t(&f);
 
53
                QString group = QString("SMSSend-%1").arg(provider);
 
54
                bool exactNumberMatch = false;
 
55
                QStringList numberWords;
 
56
                numberWords.append("Tel");
 
57
                numberWords.append("Number");
 
58
                numberWords.append("number");
 
59
                numberWords.append("TelNum");
 
60
                numberWords.append("Recipient");
 
61
                numberWords.append("Tel1");
 
62
                numberWords.append("To");
 
63
                numberWords.append("nummer");
 
64
                numberWords.append("telefone");
 
65
                numberWords.append("ToPhone");
 
66
 
 
67
                while( !t.atEnd())
 
68
                {
 
69
                        QString s = t.readLine();
 
70
                        if( s[0] == '%')
 
71
                        {
 
72
                                QStringList args = s.split(':');
 
73
                                QStringList options = args[0].split(' ');
 
74
 
 
75
                                names.append(options[0].replace(0,1,""));
 
76
 
 
77
                                bool hidden = false;
 
78
                                for(int i = 1; i < options.count(); i++)
 
79
                                        if(options[i] == "Hidden")
 
80
                                        {       hidden = true;
 
81
                                                break;
 
82
                                        }
 
83
                                isHiddens.append(hidden);
 
84
 
 
85
                                // Strip trailing whitespace in the end
 
86
                                // and '%' in the beginning
 
87
                                args[0] = args[0].simplified().mid(1);
 
88
 
 
89
                                descriptions.append(args[1]);
 
90
                                if (m_account)
 
91
                                        values.append(m_account->configGroup()->readEntry(QString("%1:%2").arg(group).arg(names[names.count()-1]),
 
92
                                                                                          QString()));
 
93
                                else
 
94
                                        values.append("");
 
95
 
 
96
                                if( args[0].contains("Message") || args[0].contains("message")
 
97
                                        || args[0].contains("message") || args[0].contains("nachricht")
 
98
                                        || args[0].contains("Msg") || args[0].contains("Mensagem") )
 
99
                                {
 
100
                                        for( int i = 0; i < options.count(); i++)
 
101
                                        {
 
102
                                                if (options[i].contains("Size="))
 
103
                                                {
 
104
                                                        QString option = options[i];
 
105
                                                        option.replace(0,5,"");
 
106
                                                        m_maxSize = option.toInt();
 
107
                                                }
 
108
                                        }
 
109
                                        messagePos = names.count()-1;
 
110
                                }
 
111
                                else if (!exactNumberMatch)
 
112
                                {
 
113
                                        for (QStringList::Iterator it=numberWords.begin(); it != numberWords.end(); ++it)
 
114
                                        {
 
115
                                                if (args[0].contains(*it))
 
116
                                                {
 
117
                                                        telPos = names.count() - 1;
 
118
                                                        if (args[0] == *it)
 
119
                                                        {
 
120
//                                                              kDebug(14160) << "Exact match for " << args[0];
 
121
                                                                exactNumberMatch = true;
 
122
                                                        }
 
123
//                                                      kDebug(14160) << "args[0] (" << args[0] << ") contains " << *it;
 
124
                                                }
 
125
                                        }
 
126
                                }
 
127
                        }
 
128
                }
 
129
        }
 
130
        f.close();
 
131
 
 
132
        if ( messagePos == -1 || telPos == -1 )
 
133
        {
 
134
                canSend = false;
 
135
                return;
 
136
        }
 
137
 
 
138
        canSend = true;
 
139
}
 
140
 
 
141
SMSSendProvider::~SMSSendProvider()
 
142
{
 
143
        kWarning( 14160 ) << "this = " << this;
 
144
}
 
145
 
 
146
void SMSSendProvider::setAccount(Kopete::Account *account)
 
147
{
 
148
        m_account = account;
 
149
}
 
150
 
 
151
QString SMSSendProvider::name(int i)
 
152
{
 
153
        if ( telPos == i || messagePos == i)
 
154
                return QString();
 
155
        else
 
156
                return names[i];
 
157
}
 
158
 
 
159
const QString& SMSSendProvider::value(int i)
 
160
{
 
161
        return values[i];
 
162
}
 
163
 
 
164
const QString& SMSSendProvider::description(int i)
 
165
{
 
166
        return descriptions[i];
 
167
}
 
168
 
 
169
bool SMSSendProvider::isHidden(int i) const
 
170
{
 
171
        return isHiddens[i];
 
172
}
 
173
 
 
174
void SMSSendProvider::save(const QList<KLineEdit*>& args)
 
175
{
 
176
        kDebug( 14160 ) << "m_account = " << m_account << " (should be non-zero!!)";
 
177
        if (!m_account) return;         // prevent crash in worst case
 
178
 
 
179
        QString group = QString("SMSSend-%1").arg(provider);
 
180
        int namesI=0;
 
181
 
 
182
        for (int i=0; i < args.count(); i++)
 
183
        {
 
184
                if (telPos == namesI || messagePos == namesI)
 
185
                {
 
186
//                  kDebug(14160) << "Skipping pos " << namesI;
 
187
                    namesI++;
 
188
                    if (telPos == namesI || messagePos == namesI)
 
189
                    {
 
190
//                      kDebug(14160) << "Skipping pos " << namesI;
 
191
                        namesI++;
 
192
                    }
 
193
                }
 
194
 
 
195
//                kDebug(14160) << "saving " << args.at(i) << " to " << names[namesI];
 
196
                if (!args.at(i)->text().isEmpty())
 
197
                {       values[namesI] = args.at(i)->text();
 
198
                        m_account->configGroup()->writeEntry(QString("%1:%2").arg(group).arg(names[namesI]), values[namesI]);
 
199
                }
 
200
                namesI++;
 
201
        }
 
202
}
 
203
 
 
204
int SMSSendProvider::count()
 
205
{
 
206
        return names.count();
 
207
}
 
208
 
 
209
void SMSSendProvider::send(const Kopete::Message& msg)
 
210
{
 
211
        if ( canSend == false )
 
212
        {
 
213
                if ( messagePos == -1 )
 
214
                {
 
215
                        canSend = false;
 
216
                        KMessageBox::error(Kopete::UI::Global::mainWidget(), i18n("Could not determine which argument should contain the message."),
 
217
                                i18n("Could Not Send Message"));
 
218
                        return;
 
219
                }
 
220
                if ( telPos == -1 )
 
221
                {
 
222
                        canSend = false;
 
223
 
 
224
                        KMessageBox::error(Kopete::UI::Global::mainWidget(), i18n("Could not determine which argument should contain the number."),
 
225
                                i18n("Could Not Send Message"));
 
226
                        return;
 
227
                }
 
228
        }
 
229
 
 
230
        m_msg = msg;
 
231
 
 
232
        QString message = msg.plainBody();
 
233
        QString nr = dynamic_cast<SMSContact *>(msg.to().first())->qualifiedNumber();
 
234
 
 
235
        if (canSend == false)
 
236
                return;
 
237
 
 
238
        values[messagePos] = message;
 
239
        values[telPos] = nr;
 
240
 
 
241
        K3Process* p = new K3Process;
 
242
 
 
243
        kWarning( 14160 ) << "Executing " << QString("%1/bin/smssend").arg(prefix) << " \"" << provider << "\" " << values.join("\" \"") << "\"";
 
244
 
 
245
        *p << QString("%1/bin/smssend").arg(prefix) << provider << values;
 
246
 
 
247
        output = "";
 
248
        connect( p, SIGNAL(processExited(K3Process*)), this, SLOT(slotSendFinished(K3Process*)));
 
249
        connect( p, SIGNAL(receivedStdout(K3Process*,char*,int)), this, SLOT(slotReceivedOutput(K3Process*,char*,int)));
 
250
//      connect( p, SIGNAL(receivedStderr(K3Process*,char*,int)), this, SLOT(slotReceivedOutput(K3Process*,char*,int)));
 
251
 
 
252
        p->start(K3Process::NotifyOnExit, K3Process::AllOutput);
 
253
}
 
254
 
 
255
void SMSSendProvider::slotSendFinished(K3Process *p)
 
256
{
 
257
        kWarning( 14160 ) << "this = " << this << ", es = " << p->exitStatus() << ", p = " << p << " (should be non-zero!!)";
 
258
        if (p->exitStatus() == 0)
 
259
                emit messageSent(m_msg);
 
260
        else
 
261
                emit messageNotSent(m_msg, QString::fromLatin1(output));
 
262
 
 
263
        p->deleteLater();
 
264
}
 
265
 
 
266
void SMSSendProvider::slotReceivedOutput(K3Process *, char *buffer, int buflen)
 
267
{
 
268
//      QStringList lines = QStringList::split("\n", QString::fromLocal8Bit(buffer, buflen));
 
269
//      for (QStringList::Iterator it = lines.begin(); it != lines.end(); ++it)
 
270
        for(int i = 0; i < buflen; i++)
 
271
                output += buffer[i];
 
272
        kWarning( 14160 ) << " output now = " << output;
 
273
}
 
274
 
 
275
int SMSSendProvider::maxSize()
 
276
{
 
277
        return m_maxSize;
 
278
}
 
279
 
 
280
#include "smssendprovider.moc"
 
281
/*
 
282
 * Local variables:
 
283
 * c-indentation-style: k&r
 
284
 * c-basic-offset: 8
 
285
 * indent-tabs-mode: t
 
286
 * End:
 
287
 */
 
288
// vim: set noet ts=4 sts=4 sw=4:
 
289