make webinterface translatable. there are around 20 short strings, all with context...
[kdenetwork.git] / kopete / protocols / sms / smsaccount.cpp
blob91d4a4b61ce3ff10f098683f992bfedec790601e
1 /* *************************************************************************
2 * copyright: (C) 2003 Richard L�k�g <nouseforaname@home.se> *
3 * copyright: (C) 2003 Gav Wood <gav@kde.org> *
4 *************************************************************************
5 */
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 *************************************************************************
17 #undef KDE_NO_COMPAT
19 #include <kconfigbase.h>
20 #include <kaction.h>
21 #include <kmenu.h>
22 #include <kdebug.h>
23 #include <kmessagebox.h>
24 #include <klocale.h>
25 #include <kopetemetacontact.h>
26 #include <kopetecontactlist.h>
28 #include "kopeteuiglobal.h"
30 #include "serviceloader.h"
32 #include "smsaccount.h"
33 #include "smsprotocol.h"
34 #include "smscontact.h"
36 SMSAccount::SMSAccount( SMSProtocol *parent, const QString &accountID, const char *name )
37 : Kopete::Account( parent, accountID )
39 Q_UNUSED(name);
41 setMyself( new SMSContact(this, accountID, accountID, Kopete::ContactList::self()->myself()) );
42 loadConfig();
43 myself()->setOnlineStatus( SMSProtocol::protocol()->SMSOffline );
45 QString sName = configGroup()->readEntry("ServiceName", QString());
46 theService = ServiceLoader::loadService(sName, this);
48 if( theService )
50 QObject::connect (theService, SIGNAL(messageSent(const Kopete::Message &)),
51 this, SLOT(slotSendingSuccess(const Kopete::Message &)));
52 QObject::connect (theService, SIGNAL(messageNotSent(const Kopete::Message &, const QString &)),
53 this, SLOT(slotSendingFailure(const Kopete::Message &, const QString &)));
54 QObject::connect (theService, SIGNAL(connected()), this, SLOT(slotConnected()));
55 QObject::connect (theService, SIGNAL(disconnected()), this, SLOT(slotDisconnected()));
60 SMSAccount::~SMSAccount()
62 delete theService;
63 theService = NULL;
66 void SMSAccount::loadConfig()
68 theSubEnable = configGroup()->readEntry("SubEnable", false);
69 theSubCode = configGroup()->readEntry("SubCode", QString());
70 theLongMsgAction = (SMSMsgAction)configGroup()->readEntry("MsgAction", 0);
73 void SMSAccount::translateNumber(QString &theNumber)
75 if(theNumber[0] == QChar('0') && theSubEnable)
76 theNumber.replace(0, 1, theSubCode);
79 const bool SMSAccount::splitNowMsgTooLong(int msgLength)
81 if( theService == NULL )
82 return false;
84 int max = theService->maxSize();
85 if(theLongMsgAction == ACT_CANCEL) return false;
86 if(theLongMsgAction == ACT_SPLIT) return true;
87 if(KMessageBox::questionYesNo(Kopete::UI::Global::mainWidget(), i18n("This message is longer than the maximum length (%1). Should it be divided to %2 messages?", max, msgLength / max + 1),
88 i18n("Message Too Long"), KGuiItem( i18n("Divide") ), KGuiItem( i18n("Do Not Divide") )) == KMessageBox::Yes)
89 return true;
90 else
91 return false;
94 void SMSAccount::setAway( bool /*away*/, const QString &)
98 void SMSAccount::connect(const Kopete::OnlineStatus&)
100 myself()->setOnlineStatus( SMSProtocol::protocol()->SMSConnecting );
101 if( theService )
102 theService->connect();
105 void SMSAccount::slotConnected()
107 myself()->setOnlineStatus( SMSProtocol::protocol()->SMSOnline );
108 setAllContactsStatus( SMSProtocol::protocol()->SMSOnline );
111 void SMSAccount::disconnect()
113 if( theService )
114 theService->disconnect();
117 void SMSAccount::slotDisconnected()
119 myself()->setOnlineStatus( SMSProtocol::protocol()->SMSOffline );
120 setAllContactsStatus( SMSProtocol::protocol()->SMSOffline );
123 void SMSAccount::slotSendMessage(Kopete::Message &msg)
125 kWarning( 14160 ) << " this = " << this;
127 if(theService == 0L)
128 return;
130 int msgLength = msg.plainBody().length();
132 if( theService->maxSize() == -1 )
134 theService->send(msg);
136 else if( theService->maxSize() < msgLength )
138 if( splitNowMsgTooLong(msgLength) )
140 for (int i=0; i < msgLength / theService->maxSize() + 1; i++)
142 QString text = msg.plainBody();
143 text = text.mid( theService->maxSize() * i, theService->maxSize() );
144 Kopete::Message m( msg.from(), msg.to() );
145 m.setPlainBody( text );
146 m.setDirection( Kopete::Message::Outbound );
148 theService->send(m);
151 else
152 slotSendingFailure(msg, i18n("Message too long."));
154 else
156 theService->send(msg);
161 void SMSAccount::slotSendingSuccess(const Kopete::Message &msg)
163 SMSContact* c = dynamic_cast<SMSContact*>(msg.to().first());
164 if( c )
165 c->slotSendingSuccess(msg);
168 void SMSAccount::slotSendingFailure(const Kopete::Message &msg, const QString &error)
170 SMSContact* c = dynamic_cast<SMSContact*>(msg.to().first());
171 if( c )
172 c->slotSendingFailure(msg, error);
175 bool SMSAccount::createContact( const QString &contactId,
176 Kopete::MetaContact * parentContact )
178 if (new SMSContact(this, contactId, parentContact->displayName(), parentContact))
179 return true;
180 else
181 return false;
184 void SMSAccount::fillActionMenu( KActionMenu *actionMenu )
186 Kopete::Account::fillActionMenu( actionMenu );
189 void SMSAccount::setOnlineStatus( const Kopete::OnlineStatus & status , const Kopete::StatusMessage &reason)
191 if ( myself()->onlineStatus().status() == Kopete::OnlineStatus::Offline && status.status() == Kopete::OnlineStatus::Online )
192 connect();
193 else if ( myself()->onlineStatus().status() != Kopete::OnlineStatus::Offline && status.status() == Kopete::OnlineStatus::Offline )
194 disconnect();
195 else if ( myself()->onlineStatus().status() != Kopete::OnlineStatus::Offline && status.status() == Kopete::OnlineStatus::Away )
196 setAway( true, reason.message() );
199 void SMSAccount::setStatusMessage( const Kopete::StatusMessage& msg )
201 Q_UNUSED(msg);
202 return;
205 SMSService* SMSAccount::service()
207 return theService;
210 #include "smsaccount.moc"