1 /* *************************************************************************
2 * copyright: (C) 2003 Richard L�k�g <nouseforaname@home.se> *
3 * copyright: (C) 2003 Gav Wood <gav@kde.org> *
4 *************************************************************************
7 /* *************************************************************************
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. *
14 *************************************************************************
19 #include <kconfigbase.h>
23 #include <kmessagebox.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
)
41 setMyself( new SMSContact(this, accountID
, accountID
, Kopete::ContactList::self()->myself()) );
43 myself()->setOnlineStatus( SMSProtocol::protocol()->SMSOffline
);
45 QString sName
= configGroup()->readEntry("ServiceName", QString());
46 theService
= ServiceLoader::loadService(sName
, this);
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()
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
)
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
)
94 void SMSAccount::setAway( bool /*away*/, const QString
&)
98 void SMSAccount::connect(const Kopete::OnlineStatus
&)
100 myself()->setOnlineStatus( SMSProtocol::protocol()->SMSConnecting
);
102 theService
->connect();
105 void SMSAccount::slotConnected()
107 myself()->setOnlineStatus( SMSProtocol::protocol()->SMSOnline
);
108 setAllContactsStatus( SMSProtocol::protocol()->SMSOnline
);
111 void SMSAccount::disconnect()
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;
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
);
152 slotSendingFailure(msg
, i18n("Message too long."));
156 theService
->send(msg
);
161 void SMSAccount::slotSendingSuccess(const Kopete::Message
&msg
)
163 SMSContact
* c
= dynamic_cast<SMSContact
*>(msg
.to().first());
165 c
->slotSendingSuccess(msg
);
168 void SMSAccount::slotSendingFailure(const Kopete::Message
&msg
, const QString
&error
)
170 SMSContact
* c
= dynamic_cast<SMSContact
*>(msg
.to().first());
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
))
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
)
193 else if ( myself()->onlineStatus().status() != Kopete::OnlineStatus::Offline
&& status
.status() == Kopete::OnlineStatus::Offline
)
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
)
205 SMSService
* SMSAccount::service()
210 #include "smsaccount.moc"