Yate

yatecbase.h

00001 /*
00002  * yatecbase.h
00003  * This file is part of the YATE Project http://YATE.null.ro
00004  *
00005  * Common base classes for all telephony clients
00006  *
00007  * Yet Another Telephony Engine - a fully featured software PBX and IVR
00008  * Copyright (C) 2004-2006 Null Team
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
00023  */
00024 
00025 #ifndef __YATECBASE_H
00026 #define __YATECBASE_H
00027 
00028 #ifndef __cplusplus
00029 #error C++ is required
00030 #endif
00031 
00032 #include <yatephone.h>
00033 
00037 namespace TelEngine {
00038 
00039 class Window;                            // A generic window
00040 class UIWidget;                          // A custom widget
00041 class UIFactory;                         // Base factory used to build custom widgets
00042 class Client;                            // The client
00043 class ClientChannel;                     // A client telephony channel
00044 class ClientDriver;                      // The client telephony driver
00045 class ClientLogic;                       // Base class for all client logics
00046 class DefaultLogic;                      // The client's default logic
00047 class ClientAccount;                     // A client account
00048 class ClientAccountList;                 // A client account list
00049 class ClientContact;                     // A client contact
00050 class ClientResource;                    // A client contact's resource
00051 class DurationUpdate;                    // Class used to update UI durations
00052 class ClientSound;                       // A sound file
00053 
00054 
00060 class YATE_API Window : public GenObject
00061 {
00062     friend class Client;
00063 public:
00068     Window(const char* id = 0);
00069 
00073     virtual ~Window();
00074 
00079     virtual const String& toString() const;
00080 
00081     /*
00082      * Get the window's title (may not be displayed on screen)
00083      * @return Title of this window
00084      */
00085     virtual void title(const String& text);
00086 
00091     virtual void context(const String& text);
00092 
00098     virtual bool setParams(const NamedList& params);
00099 
00104     virtual void setOver(const Window* parent) = 0;
00105 
00111     virtual bool hasElement(const String& name) = 0;
00112 
00119     virtual bool setActive(const String& name, bool active) = 0;
00120 
00127     virtual bool setFocus(const String& name, bool select = false) = 0;
00128 
00135     virtual bool setShow(const String& name, bool visible) = 0;
00136 
00144     virtual bool setText(const String& name, const String& text,
00145         bool richText = false) = 0;
00146 
00153     virtual bool setCheck(const String& name, bool checked) = 0;
00154 
00161     virtual bool setSelect(const String& name, const String& item) = 0;
00162 
00169     virtual bool setUrgent(const String& name, bool urgent) = 0;
00170 
00177     virtual bool hasOption(const String& name, const String& item) = 0;
00178 
00187     virtual bool addOption(const String& name, const String& item, bool atStart = false,
00188         const String& text = String::empty()) = 0;
00189 
00196     virtual bool getOptions(const String& name, NamedList* items) = 0;
00197 
00204     virtual bool delOption(const String& name, const String& item) = 0;
00205 
00214     virtual bool addLines(const String& name, const NamedList* lines, unsigned int max, 
00215         bool atStart = false);
00216 
00225     virtual bool addTableRow(const String& name, const String& item,
00226         const NamedList* data = 0, bool atStart = false);
00227 
00235     virtual bool setMultipleRows(const String& name, const NamedList& data, const String& prefix = String::empty());
00236 
00245     virtual bool insertTableRow(const String& name, const String& item,
00246         const String& before, const NamedList* data = 0);
00247 
00254     virtual bool delTableRow(const String& name, const String& item);
00255 
00263     virtual bool setTableRow(const String& name, const String& item, const NamedList* data);
00264 
00273     virtual bool updateTableRow(const String& name, const String& item,
00274         const NamedList* data = 0, bool atStart = false);
00275 
00287     virtual bool updateTableRows(const String& name, const NamedList* data,
00288         bool atStart = false);
00289 
00297     virtual bool getTableRow(const String& name, const String& item, NamedList* data = 0);
00298 
00304     virtual bool clearTable(const String& name);
00305 
00313     virtual bool getText(const String& name, String& text, bool richText = false) = 0;
00314 
00321     virtual bool getCheck(const String& name, bool& checked) = 0;
00322 
00329     virtual bool getSelect(const String& name, String& item) = 0;
00330 
00338     virtual bool setProperty(const String& name, const String& item, const String& value)
00339         { return false; }
00340 
00348     virtual bool getProperty(const String& name, const String& item, String& value)
00349         { return false; }
00350 
00354     inline void populate() {
00355             if (m_populated)
00356                 return;
00357             doPopulate();
00358             m_populated = true;
00359         }
00360 
00364     inline void init() {
00365             if (m_initialized)
00366                 return;
00367             doInit();
00368             m_initialized = true;
00369         }
00370 
00374     virtual void show() = 0;
00375 
00379     virtual void hide() = 0;
00380 
00386     virtual void size(int width, int height) = 0;
00387 
00393     virtual void move(int x, int y) = 0;
00394 
00400     virtual void moveRel(int dx, int dy) = 0;
00401 
00407     virtual bool related(const Window* wnd) const;
00408 
00409     virtual void menu(int x, int y) = 0;
00410 
00415     virtual bool canClose()
00416         { return true; }
00417 
00422     inline const String& id() const
00423         { return m_id; }
00424 
00425     /*
00426      * Get the window's title (may not be displayed on screen)
00427      * @return Title of this window
00428      */
00429     inline const String& title() const
00430         { return m_title; }
00431 
00436     inline const String& context() const
00437         { return m_context; }
00438 
00443     inline bool visible() const
00444         { return m_visible; }
00445 
00450     inline void visible(bool yes)
00451         { if (yes) show(); else hide(); }
00452 
00457     inline bool master() const
00458         { return m_master; }
00459 
00464     inline bool popup() const
00465         { return m_popup; }
00466 
00473     static bool isValidParamPrefix(const String& prefix);
00474 
00475 protected:
00476     virtual void doPopulate() = 0;
00477     virtual void doInit() = 0;
00478 
00479     String m_id;
00480     String m_title;
00481     String m_context;
00482     bool m_visible;
00483     bool m_master;
00484     bool m_popup;
00485     bool m_saveOnClose;                  // Save window's data when destroyed
00486 
00487 private:
00488     bool m_populated;                    // Already populated flag
00489     bool m_initialized;                  // Already initialized flag
00490 };
00491 
00492 class YATE_API UIWidget : public String
00493 {
00494 public:
00499     inline UIWidget(const char* name = 0)
00500         : String(name)
00501         {}
00502 
00506     virtual ~UIWidget()
00507         {}
00508 
00513     inline const String& name() const
00514         { return toString(); }
00515 
00521     virtual bool setParams(const NamedList& params)
00522         { return false; }
00523 
00529     virtual bool getOptions(NamedList& items)
00530         { return false; }
00531 
00539     virtual bool addTableRow(const String& item, const NamedList* data = 0,
00540         bool atStart = false)
00541         { return false; }
00542 
00548     virtual bool setMultipleRows(const NamedList& data, const String& prefix = String::empty())
00549         { return false; }
00550 
00561     virtual bool updateTableRows(const NamedList* data, bool atStart = false)
00562         { return false; }
00563 
00571     virtual bool insertTableRow(const String& item, const String& before,
00572         const NamedList* data = 0)
00573         { return false; }
00574 
00580     virtual bool delTableRow(const String& item)
00581         { return false; }
00582 
00589     virtual bool setTableRow(const String& item, const NamedList* data)
00590         { return false; }
00591 
00598     virtual bool getTableRow(const String& item, NamedList* data = 0)
00599         { return false; }
00600 
00605     virtual bool clearTable()
00606         { return false; }
00607 
00613     virtual bool setSelect(const String& item)
00614         { return false; }
00615 
00621     virtual bool getSelect(String& item)
00622         { return false; }
00623 };
00624 
00630 class YATE_API UIFactory : public String
00631 {
00632 public:
00636     UIFactory(const char* name);
00637 
00641     virtual ~UIFactory();
00642 
00648     inline bool canBuild(const String& type)
00649         { return 0 != m_types.find(type); }
00650 
00658     virtual void* create(const String& type, const char* name, NamedList* params = 0) = 0;
00659 
00669     static void* build(const String& type, const char* name, NamedList* params = 0,
00670         const char* factory = 0);
00671 
00672 protected:
00673     ObjList m_types;                     // List of object types this factory can build
00674 
00675 private:
00676     static ObjList s_factories;          // Registered factories list
00677 };
00678 
00683 class YATE_API Client : public Thread, public MessageReceiver
00684 {
00685     friend class Window;
00686     friend class ClientChannel;
00687     friend class ClientDriver;
00688     friend class ClientLogic;
00689 public:
00693     enum MsgID {
00694         CallCdr            = 0,
00695         UiAction           = 1,
00696         UserLogin          = 2,
00697         UserNotify         = 3,
00698         ResourceNotify     = 4,
00699         ResourceSubscribe  = 5,
00700         ClientChanUpdate   = 7,
00701         // Handlers not automatically installed
00702         ChanNotify         = 8,
00703         // NOTE: Keep the MsgIdCount in sync: it can be used by other parties to install
00704         //  other relays
00705         MsgIdCount         = 9
00706     };
00707 
00711     enum ClientToggle {
00712         OptMultiLines             = 0,   // Accept incoming calls
00713         OptAutoAnswer             = 1,   // Auto answer incoming calls
00714         OptRingIn                 = 2,   // Enable/disable incoming ringer
00715         OptRingOut                = 3,   // Enable/disable outgoing ringer
00716         OptActivateLastOutCall    = 4,   // Set the last outgoing call active
00717         OptActivateLastInCall     = 5,   // Set the last incoming call active
00718         OptActivateCallOnSelect   = 6,   // Set the active call when selected in channel list (don't require double click)
00719         OptKeypadVisible          = 7,   // Show/hide keypad
00720         OptOpenIncomingUrl        = 8,   // Open an incoming URL in call.execute message
00721         OptCount                  = 9
00722     };
00723 
00728     Client(const char *name = 0);
00729 
00733     virtual ~Client();
00734 
00738     virtual void run();
00739 
00743     virtual void cleanup();
00744 
00748     virtual void main() = 0;
00749 
00753     virtual void lock() = 0;
00754 
00758     virtual void unlock() = 0;
00759 
00763     inline void lockOther()
00764         { if (!m_oneThread) lock(); }
00765 
00769     inline void unlockOther()
00770         { if (!m_oneThread) unlock(); }
00771 
00775     virtual void allHidden() = 0;
00776 
00782     void loadUI(const char* file = 0, bool init = true);
00783 
00787     virtual void quit() = 0;
00788 
00794     bool openUrlSafe(const String& url);
00795 
00801     virtual bool openUrl(const String& url) = 0;
00802 
00809     virtual bool received(Message& msg, int id);
00810 
00818     virtual bool createWindowSafe(const String& name,
00819         const String& alias = String::empty());
00820 
00829     virtual bool createObject(void** dest, const String& type, const char* name,
00830         NamedList* params = 0);
00831 
00838     virtual bool closeWindow(const String& name, bool hide = true);
00839 
00845     virtual bool debugHook(bool active);
00846 
00852     virtual bool addToLog(const String& text);
00853 
00860     virtual bool setStatus(const String& text, Window* wnd = 0);
00861 
00868     bool setStatusLocked(const String& text, Window* wnd = 0);
00869 
00877     bool setParams(const NamedList* params, Window* wnd = 0, Window* skip = 0);
00878 
00887     virtual bool action(Window* wnd, const String& name, NamedList* params = 0);
00888 
00897     virtual bool toggle(Window* wnd, const String& name, bool active);
00898 
00908     virtual bool select(Window* wnd, const String& name, const String& item, const String& text = String::empty());
00909 
00914     inline bool oneThread() const
00915         { return m_oneThread; }
00916 
00921     inline int line() const
00922         { return m_line; }
00923 
00928     void line(int newLine);
00929 
00930     bool hasElement(const String& name, Window* wnd = 0, Window* skip = 0);
00931     bool setActive(const String& name, bool active, Window* wnd = 0, Window* skip = 0);
00932     bool setFocus(const String& name, bool select = false, Window* wnd = 0, Window* skip = 0);
00933     bool setShow(const String& name, bool visible, Window* wnd = 0, Window* skip = 0);
00934     bool setText(const String& name, const String& text, bool richText = false, 
00935         Window* wnd = 0, Window* skip = 0);
00936     bool setCheck(const String& name, bool checked, Window* wnd = 0, Window* skip = 0);
00937     bool setSelect(const String& name, const String& item, Window* wnd = 0, Window* skip = 0);
00938     bool setUrgent(const String& name, bool urgent, Window* wnd = 0, Window* skip = 0);
00939     bool hasOption(const String& name, const String& item, Window* wnd = 0, Window* skip = 0);
00940 
00949     virtual bool getOptions(const String& name, NamedList* items,
00950         Window* wnd = 0, Window* skip = 0);
00951 
00952     bool addOption(const String& name, const String& item, bool atStart,
00953         const String& text = String::empty(), Window* wnd = 0, Window* skip = 0);
00954     bool delOption(const String& name, const String& item, Window* wnd = 0, Window* skip = 0);
00955 
00966     bool addLines(const String& name, const NamedList* lines, unsigned int max, 
00967         bool atStart = false, Window* wnd = 0, Window* skip = 0);
00968 
00969     bool addTableRow(const String& name, const String& item, const NamedList* data = 0,
00970         bool atStart = false, Window* wnd = 0, Window* skip = 0);
00971 
00980     bool setMultipleRows(const String& name, const NamedList& data, const String& prefix = String::empty(), Window* wnd = 0, Window* skip = 0);
00981 
00992     bool insertTableRow(const String& name, const String& item,
00993         const String& before, const NamedList* data = 0,
00994         Window* wnd = 0, Window* skip = 0);
00995 
00996     bool delTableRow(const String& name, const String& item, Window* wnd = 0, Window* skip = 0);
00997     bool setTableRow(const String& name, const String& item, const NamedList* data,
00998         Window* wnd = 0, Window* skip = 0);
00999     bool getTableRow(const String& name, const String& item, NamedList* data = 0,
01000         Window* wnd = 0, Window* skip = 0);
01001     bool clearTable(const String& name, Window* wnd = 0, Window* skip = 0);
01002 
01013     bool updateTableRow(const String& name, const String& item, const NamedList* data = 0,
01014         bool atStart = false, Window* wnd = 0, Window* skip = 0);
01015 
01029     bool updateTableRows(const String& name, const NamedList* data, bool atStart = false,
01030         Window* wnd = 0, Window* skip = 0);
01031 
01041     bool getText(const String& name, String& text, bool richText = false, Window* wnd = 0, Window* skip = 0);
01042 
01043     bool getCheck(const String& name, bool& checked, Window* wnd = 0, Window* skip = 0);
01044     bool getSelect(const String& name, String& item, Window* wnd = 0, Window* skip = 0);
01045 
01055     virtual bool setProperty(const String& name, const String& item, const String& value,
01056         Window* wnd = 0, Window* skip = 0);
01057 
01067     virtual bool getProperty(const String& name, const String& item, String& value,
01068         Window* wnd = 0, Window* skip = 0);
01069 
01070     void moveRelated(const Window* wnd, int dx, int dy);
01071     inline bool initialized() const
01072         { return m_initialized; }
01073     inline static Client* self()
01074         { return s_client; }
01075 
01080     static inline bool valid()
01081         { return self() && (self() == Thread::current() || !(exiting() || Engine::exiting())); }
01082 
01083     inline static bool changing()
01084         { return (s_changing > 0); }
01085     static Window* getWindow(const String& name);
01086     static bool setVisible(const String& name, bool show = true);
01087     static bool getVisible(const String& name);
01088     static bool openPopup(const String& name, const NamedList* params = 0, const Window* parent = 0);
01089     static bool openMessage(const char* text, const Window* parent = 0, const char* context = 0);
01090     static bool openConfirm(const char* text, const Window* parent = 0, const char* context = 0);
01091     static ObjList* listWindows();
01092     void idleActions();
01093 
01101     bool postpone(const Message& msg, int id, bool copyUserData = false);
01102 
01111     virtual bool chooseFile(Window* parent, NamedList& params)
01112         { return false; }
01113 
01124     virtual bool setClientParam(const String& param, const String& value,
01125         bool save, bool update);
01126 
01133     virtual bool backspace(const String& name, Window* wnd = 0);
01134 
01142     void installRelay(const char* name, int id, int prio);
01143 
01148     virtual bool callRouting(Message& msg)
01149         { return true;}
01150 
01155     virtual bool imRouting(Message& msg)
01156         { return true;}
01157 
01162     virtual bool imExecute(Message& msg);
01163 
01174     virtual bool buildIncomingChannel(Message& msg, const String& dest);
01175 
01181     virtual bool buildOutgoingChannel(NamedList& params);
01182 
01190     bool callIncoming(Message& msg, const String& dest);
01191 
01198     void callAnswer(const String& id, bool setActive = true);
01199 
01207     void callTerminate(const String& id, const char* reason = 0, const char* error = 0);
01208 
01213     ClientChannel* getActiveChannel();
01214 
01222     virtual bool ringer(bool in, bool on);
01223 
01231     virtual bool createSound(const char* name, const char* file, const char* device = 0)
01232         { return false; }
01233 
01240     bool emitDigits(const char* digits, const String& id = String::empty());
01241 
01248     inline bool emitDigit(char digit, const String& id = String::empty()) {
01249             char s[2] = {digit,0}; 
01250             return emitDigits(s,id);
01251         }
01252 
01258     inline bool getBoolOpt(ClientToggle toggle)
01259         { return toggle < OptCount ? m_toggles[toggle] : false; }
01260 
01268     bool setBoolOpt(ClientToggle toggle, bool value, bool updateUi = false);
01269 
01278     virtual bool formatDateTime(String& dest, unsigned int secs, const char* format,
01279         bool utc = false)
01280         { return false; }
01281 
01286     virtual void engineStart(Message& msg);
01287 
01292     static inline bool exiting()
01293         { return s_exiting; }
01294 
01300     static bool addLogic(ClientLogic* logic);
01301 
01306     static void removeLogic(ClientLogic* logic);
01307 
01313     static ClientLogic* findLogic(const String& name);
01314 
01323     static Message* eventMessage(const String& event, Window* wnd = 0,
01324         const char* name = 0, NamedList* params = 0);
01325 
01333     static bool save(Configuration& cfg, Window* parent = 0, bool showErr = true);
01334 
01340     static ClientToggle getBoolOpt(const String& name);
01341 
01345     static inline void setLogicsTick()
01346         { s_idleLogicsTick = true; }
01347 
01348     static Configuration s_settings;     // Client settings
01349     static Configuration s_actions;      // Logic preferrences
01350     static Configuration s_accounts;     // Accounts
01351     static Configuration s_contacts;     // Contacts
01352     static Configuration s_providers;    // Provider settings
01353     static Configuration s_history;      // Call log
01354     static Configuration s_calltoHistory;  // Dialed destinations history
01355     // Holds a not selected/set value match
01356     static Regexp s_notSelected;
01357     // Paths
01358     static String s_skinPath;
01359     static String s_soundPath;
01360     // Ring name for incoming channels
01361     static String s_ringInName;
01362     // Ring name for outgoing channels
01363     static String s_ringOutName;
01364     // Status widget's name
01365     static String s_statusWidget;
01366     // Widget displaying the debug text
01367     static String s_debugWidget;
01368     // The list of cient's toggles
01369     static String s_toggles[OptCount];
01370 
01371 protected:
01377     virtual ClientLogic* createDefaultLogic();
01378     virtual bool createWindow(const String& name,
01379         const String& alias = String::empty()) = 0;
01380     virtual void loadWindows(const char* file = 0) = 0;
01381     virtual void initWindows();
01382     virtual void initClient();
01383     virtual void exitClient()
01384         {}
01385     inline bool needProxy() const
01386         { return m_oneThread && !isCurrent(); }
01387     bool driverLockLoop();
01388     static bool driverLock(long maxwait = 0);
01389     static void driverUnlock();
01390 
01391     static bool s_exiting;               // Exiting flag
01392 
01393     ObjList m_windows;
01394     bool m_initialized;
01395     int m_line;
01396     bool m_oneThread;
01397     bool m_toggles[OptCount];
01398     ObjList m_relays;                    // Message relays installed by this receiver
01399     ClientLogic* m_defaultLogic;         // The default logic
01400     static Client* s_client;
01401     static int s_changing;
01402     static ObjList s_logics;
01403     static bool s_idleLogicsTick;        // Call logics' timerTick()
01404 };
01405 
01410 class YATE_API ClientChannel : public Channel
01411 {
01412     friend class ClientDriver;
01413 public:
01417     enum Notification {
01418         Startup,
01419         Destroyed,
01420         Active,
01421         OnHold,
01422         Mute,
01423         Noticed,
01424         AddrChanged,
01425         Routed,
01426         Accepted,
01427         Rejected,
01428         Progressing,
01429         Ringing,
01430         Answered,
01431         Transfer,
01432         Conference,
01433         Unknown
01434     };
01435 
01441     ClientChannel(const Message& msg, const String& peerid);
01442 
01448     ClientChannel(const String& target, const NamedList& params);
01449 
01454     ClientChannel(const String& soundId);
01455 
01456     virtual ~ClientChannel();
01457 
01464     bool start(const String& target, const NamedList& params);
01465 
01466     virtual bool msgProgress(Message& msg);
01467     virtual bool msgRinging(Message& msg);
01468     virtual bool msgAnswered(Message& msg);
01469     virtual bool msgDrop(Message& msg, const char* reason);
01470     virtual bool callRouted(Message& msg);
01471     virtual void callAccept(Message& msg);
01472     virtual void callRejected(const char* error, const char* reason, const Message* msg);
01473 
01478     void callAnswer(bool setActive = true);
01479 
01484     inline const String& party() const
01485         { return m_party; }
01486 
01491     inline bool conference() const
01492         { return m_conference; }
01493 
01498     inline const String& transferId() const
01499         { return m_transferId; }
01500 
01505     inline RefObject* clientData() const
01506         { return m_clientData; }
01507 
01513     inline void setClientData(RefObject* obj = 0) {
01514             TelEngine::destruct(m_clientData);
01515             if (obj && obj->ref())
01516                 m_clientData = obj;
01517         }
01518 
01525     bool setMedia(bool open = false, bool replace = false);
01526 
01533     bool setActive(bool active, bool update = true);
01534 
01541     bool setMuted(bool on, bool update = true);
01542 
01548     void setTransfer(const String& target = String::empty());
01549 
01555     void setConference(const String& target = String::empty());
01556 
01561     inline const String& peerOutFormat() const
01562         { return m_peerOutFormat; }
01563 
01568     inline const String& peerInFormat() const
01569         { return m_peerInFormat; }
01570 
01575     inline bool active() const
01576         { return m_active; }
01577 
01582     inline bool muted() const
01583         { return m_muted; }
01584 
01589     inline bool isNoticed() const
01590         { return m_noticed; }
01591 
01595     void noticed();
01596 
01601     inline int line() const
01602         { return m_line; }
01603 
01608     void line(int newLine);
01609 
01620     void update(int notif, bool chan = true,
01621         bool updatePeer = true, const char* engineMsg = 0,
01622         bool minimal = false, bool data = false);
01623 
01630     static int lookup(const char* notif, int def = Unknown)
01631         { return TelEngine::lookup(notif,s_notification,def); }
01632 
01639     static const char* lookup(int notif, const char* def = 0)
01640         { return TelEngine::lookup(notif,s_notification,def); }
01641 
01645     static TokenDict s_notification[];
01646 
01647 protected:
01648     virtual void destroyed();
01649     virtual void connected(const char* reason);
01650     virtual void disconnected(bool final, const char* reason);
01651     // Check for a source in channel's peer or a received message's user data
01652     inline bool peerHasSource(Message& msg) {
01653             CallEndpoint* ch = getPeer();
01654             if (!ch)
01655                 ch = static_cast<CallEndpoint*>(msg.userObject("CallEndpoint"));
01656             return ch && ch->getSource();
01657         }
01658     // Check if our consumer's source sent any data
01659     // Don't set the silence flag is already reset
01660     void checkSilence();
01661 
01662     String m_party;                      // Remote party
01663     String m_peerOutFormat;              // Peer consumer's data format
01664     String m_peerInFormat;               // Peer source's data format
01665     String m_reason;                     // Termination reason
01666     String m_peerId;                     // Peer's id (used to re-connect)
01667     bool m_noticed;                      // Incoming channel noticed flag
01668     int m_line;                          // Channel's line (address)
01669     bool m_active;                       // Channel active flag
01670     bool m_silence;                      // True if the peer did't sent us any audio data
01671     bool m_conference;                   // True if this channel is in conference
01672     bool m_muted;                        // True if this channel is muted (no data source))
01673     String m_transferId;                 // Transferred id or empty if not transferred
01674     RefObject* m_clientData;             // Obscure data used by client logics
01675     bool m_utility;                      // Regular client channel flag
01676     String m_soundId;                    // The id of the sound to play
01677 };
01678 
01683 class YATE_API ClientDriver : public Driver
01684 {
01685     friend class ClientChannel;          // Reset active channel's id
01686 public:
01687     ClientDriver();
01688     virtual ~ClientDriver();
01689     virtual void initialize() = 0;
01690     virtual bool msgExecute(Message& msg, String& dest);
01691     virtual void msgTimer(Message& msg);
01692     virtual bool msgRoute(Message& msg);
01693     virtual bool received(Message& msg, int id);
01694 
01699     inline const String& activeId() const
01700         { return m_activeId; }
01701 
01710     bool setActive(const String& id = String::empty());
01711 
01717     ClientChannel* findLine(int line);
01718 
01723     inline static ClientDriver* self()
01724         { return s_driver; }
01725 
01730     inline static const String& device()
01731         { return s_device; }
01732 
01737     static void dropCalls(const char* reason = 0);
01738 
01745     static bool setAudioTransfer(const String& id, const String& target = String::empty());
01746 
01755     static bool setConference(const String& id, bool in, const String* confName = 0);
01756 
01762     static ClientChannel* findChan(const String& id);
01763 
01769     static ClientChannel* findChanByPeer(const String& peer);
01770 
01775     static ClientChannel* findActiveChan()
01776         { return self() ? findChan(self()->activeId()) : 0; }
01777 
01781     static String s_confName;
01782 
01787     static bool s_dropConfPeer;
01788 
01789 protected:
01790     void setup();
01791     static ClientDriver* s_driver;
01792     static String s_device;
01793     String m_activeId;                   // The active channel's id
01794 };
01795 
01802 class YATE_API ClientLogic : public GenObject
01803 {
01804     friend class Client;
01805 public:
01809     virtual ~ClientLogic();
01810 
01815     inline const String& name() const
01816         { return m_name; }
01817 
01822     inline int priority() const
01823         { return m_prio; }
01824 
01829     virtual const String& toString() const;
01830 
01836     bool setParams(const NamedList& params);
01837 
01845     virtual bool action(Window* wnd, const String& name, NamedList* params = 0)
01846         { return false; }
01847 
01855     virtual bool toggle(Window* wnd, const String& name, bool active)
01856         { return false; }
01857 
01866     virtual bool select(Window* wnd, const String& name, const String& item,
01867         const String& text = String::empty())
01868         { return false; }
01869 
01878     virtual bool setClientParam(const String& param, const String& value,
01879         bool save, bool update)
01880         { return false; }
01881 
01886     virtual bool imIncoming(Message& msg)
01887         { return false; }
01888 
01896     virtual bool callIncoming(Message& msg, const String& dest)
01897         { return false; }
01898 
01906     virtual bool callStart(NamedList& params, Window* wnd = 0)
01907         { return false; }
01908 
01915     virtual bool line(const String& name, Window* wnd = 0);
01916 
01924     virtual bool display(NamedList& params, bool widget, Window* wnd = 0);
01925 
01932     virtual bool backspace(const String& name, Window* wnd = 0);
01933 
01940     virtual bool command(const String& name, Window* wnd = 0);
01941 
01952     virtual bool debug(const String& name, bool active, Window* wnd = 0);
01953 
01961     virtual bool editAccount(bool newAcc, NamedList* params, Window* wnd = 0)
01962         { return false; }
01963 
01970     virtual bool acceptAccount(NamedList* params, Window* wnd = 0)
01971         { return false; }
01972 
01979     virtual bool delAccount(const String& account, Window* wnd = 0)
01980         { return false; }
01981 
01989     virtual bool updateAccount(const NamedList& account, bool login, bool save)
01990         { return false; }
01991 
01998     virtual bool loginAccount(const NamedList& account, bool login)
01999         { return false; }
02000 
02009     virtual bool updateContact(const NamedList& contact, bool save, bool update)
02010         { return false; }
02011 
02018     virtual bool acceptContact(NamedList* params, Window* wnd = 0)
02019         { return false; }
02020 
02028     virtual bool editContact(bool newCont, NamedList* params = 0, Window* wnd = 0)
02029         { return false; }
02030 
02037     virtual bool delContact(const String& contact, Window* wnd = 0)
02038         { return false; }
02039 
02046     virtual bool callContact(NamedList* params = 0, Window* wnd = 0)
02047         { return false; }
02048 
02056     virtual bool updateProviders(const NamedList& provider, bool save, bool update)
02057         { return false; }
02058 
02066     virtual bool callLogUpdate(NamedList& params, bool save, bool update)
02067         { return false; }
02068 
02077     virtual bool callLogClear(const String& table, const String& direction)
02078         { return false; }
02079 
02085     virtual bool callLogCall(const String& billid)
02086         { return false; }
02087 
02093     virtual bool callLogCreateContact(const String& billid)
02094         { return false; }
02095 
02102     virtual bool help(const String& action, Window* wnd)
02103         { return false; }
02104 
02109     virtual bool calltoLoaded()
02110         { return false; }
02111 
02115     virtual void loadedWindows()
02116         {}
02117 
02121     virtual void initializedWindows()
02122         {}
02123 
02130     virtual bool initializedClient()
02131         { return false; }
02132 
02137     virtual void exitingClient()
02138         {}
02139 
02146     virtual bool handleUiAction(Message& msg, bool& stopLogic)
02147         { return false; }
02148 
02155     virtual bool handleCallCdr(Message& msg, bool& stopLogic)
02156         { return false; }
02157 
02164     virtual bool handleUserLogin(Message& msg, bool& stopLogic)
02165         { return false; }
02166 
02173     virtual bool handleUserNotify(Message& msg, bool& stopLogic)
02174         { return false; }
02175 
02182     virtual bool handleResourceNotify(Message& msg, bool& stopLogic)
02183         { return false; }
02184 
02191     virtual bool handleResourceSubscribe(Message& msg, bool& stopLogic)
02192         { return false; }
02193 
02200     virtual bool handleClientChanUpdate(Message& msg, bool& stopLogic)
02201         { return false; }
02202 
02212     virtual bool defaultMsgHandler(Message& msg, int id, bool& stopLogic)
02213         { return false; }
02214 
02219     virtual void engineStart(Message& msg)
02220         {}
02221 
02228     virtual bool addDurationUpdate(DurationUpdate* duration, bool autoDelete = false);
02229 
02236     virtual bool removeDurationUpdate(const String& name, bool delObj = false);
02237 
02244     virtual bool removeDurationUpdate(DurationUpdate* duration, bool delObj = false);
02245 
02252     virtual DurationUpdate* findDurationUpdate(const String& name, bool ref = true);
02253 
02257     virtual void clearDurationUpdate();
02258 
02262     virtual void destruct();
02263 
02268     static void initStaticData();
02269 
02270     // Account options string list
02271     static ObjList s_accOptions;
02272     // Parameters that are applied from provider template
02273     static const char* s_provParams[];
02274 
02275 protected:
02281     ClientLogic(const char* name, int priority);
02282 
02288     virtual void idleTimerTick(Time& time)
02289         {}
02290 
02291     // The list of protocols supported by the client
02292     static ObjList s_protocols;
02293     // Mutext used to lock protocol list
02294     static Mutex s_protocolsMutex;
02295 
02296     ObjList m_durationUpdate;            // Duration updates
02297     Mutex m_durationMutex;               // Lock duration operations
02298 
02299 private:
02300     ClientLogic() {}                     // No default constructor
02301 
02302     String m_name;                       // Logic's name
02303     int m_prio;                          // Logics priority
02304 };
02305 
02306 
02311 class YATE_API DefaultLogic : public ClientLogic
02312 {
02313 public:
02319     DefaultLogic(const char* name = "default", int prio = -100);
02320 
02328     virtual bool action(Window* wnd, const String& name, NamedList* params = 0);
02329 
02337     virtual bool toggle(Window* wnd, const String& name, bool active);
02338 
02347     virtual bool select(Window* wnd, const String& name, const String& item,
02348         const String& text = String::empty());
02349 
02358     virtual bool setClientParam(const String& param, const String& value,
02359         bool save, bool update);
02360 
02365     virtual bool imIncoming(Message& msg)
02366         { return false; }
02367 
02375     virtual bool callIncoming(Message& msg, const String& dest)
02376         { return Client::self() && Client::self()->buildIncomingChannel(msg,dest); }
02377 
02385     virtual bool callStart(NamedList& params, Window* wnd = 0);
02386 
02394     virtual bool digitPressed(NamedList& params, Window* wnd = 0);
02395 
02403     virtual bool editAccount(bool newAcc, NamedList* params, Window* wnd = 0);
02404 
02411     virtual bool acceptAccount(NamedList* params, Window* wnd = 0);
02412 
02419     virtual bool delAccount(const String& account, Window* wnd = 0);
02420 
02428     virtual bool updateAccount(const NamedList& account, bool login, bool save);
02429 
02436     virtual bool loginAccount(const NamedList& account, bool login);
02437 
02446     virtual bool updateContact(const NamedList& contact, bool save, bool update);
02447 
02454     virtual bool acceptContact(NamedList* params, Window* wnd = 0);
02455 
02463     virtual bool editContact(bool newCont, NamedList* params = 0, Window* wnd = 0);
02464 
02471     virtual bool delContact(const String& contact, Window* wnd = 0);
02472 
02479     virtual bool callContact(NamedList* params = 0, Window* wnd = 0);
02480 
02488     virtual bool updateProviders(const NamedList& provider, bool save, bool update);
02489 
02497     virtual bool callLogUpdate(NamedList& params, bool save, bool update);
02498 
02507     virtual bool callLogClear(const String& table, const String& direction);
02508 
02514     virtual bool callLogCall(const String& billid);
02515 
02521     virtual bool callLogCreateContact(const String& billid);
02522 
02529     virtual bool help(const String& action, Window* wnd);
02530 
02535     virtual bool calltoLoaded();
02536 
02540     virtual void loadedWindows()
02541         {}
02542 
02546     virtual void initializedWindows();
02547 
02554     virtual bool initializedClient();
02555 
02560     virtual void exitingClient();
02561 
02568     virtual bool handleUiAction(Message& msg, bool& stopLogic);
02569 
02576     virtual bool handleCallCdr(Message& msg, bool& stopLogic);
02577 
02584     virtual bool handleUserLogin(Message& msg, bool& stopLogic);
02585 
02592     virtual bool handleUserNotify(Message& msg, bool& stopLogic);
02593 
02600     virtual bool handleResourceNotify(Message& msg, bool& stopLogic);
02601 
02608     virtual bool handleResourceSubscribe(Message& msg, bool& stopLogic);
02609 
02616     virtual bool handleClientChanUpdate(Message& msg, bool& stopLogic);
02617 
02627     virtual bool defaultMsgHandler(Message& msg, int id, bool& stopLogic);
02628 
02634     virtual void updateSelectedChannel(const String* item = 0);
02635 
02640     virtual void engineStart(Message& msg)
02641         {}
02642 
02643 protected:
02649     virtual void idleTimerTick(Time& time);
02650 
02656     virtual bool enableCallActions(const String& id);
02657 
02664     virtual bool fillCallStart(NamedList& p, Window* wnd = 0);
02665 
02671     virtual void channelSelectionChanged(const String& old);
02672 
02673     String m_selectedChannel;            // The currently selected channel
02674     String m_transferInitiated;          // Tranfer initiated id
02675     bool m_accShowAdvanced;              // Show/hide the account advanced options
02676 };
02677 
02678 
02679 class YATE_API ClientAccount : public RefObject, public Mutex
02680 {
02681     friend class ClientContact;
02682 public:
02690     ClientAccount(const char* proto, const char* user,
02691         const char* host, bool startup);
02692 
02697     ClientAccount(const NamedList& params);
02698 
02703     inline const URI& uri() const
02704         { return m_uri; }
02705 
02710     inline const URI& id() const
02711         { return m_id; }
02712 
02717     inline ObjList& contacts()
02718         { return m_contacts; }
02719 
02724     virtual const String& toString() const
02725         { return m_id; }
02726 
02731     ClientResource* resource(bool ref = false);
02732 
02737     void setResource(ClientResource* res = 0);
02738 
02745     virtual ClientContact* findContact(const String& id, bool ref = false);
02746 
02754     virtual ClientContact* findContact(const String& id, const String& resid, 
02755         bool ref = false);
02756 
02763     virtual ClientContact* appendContact(const String& id, const char* name);
02764 
02770     virtual ClientContact* appendContact(const NamedList& params);
02771 
02778     virtual ClientContact* removeContact(const String& id, bool delObj = true);
02779 
02786     virtual Message* userlogin(bool login, const char* msg = "user.login");
02787 
02795     static void buildAccountId(URI& dest, const char* proto, const char* user, const char* host) {
02796             URI u(proto,user,host);
02797             dest = u.toLower();
02798         }
02799 
02800     String m_password;                   // Account's password
02801     String m_server;                     // Account's server (name or IP address)
02802     int m_port;                          // Server's port used to connect to
02803     String m_options;                    // Account's options
02804     bool m_startup;                      // Enable/disable flag
02805     String m_outbound;                   // Outbound server (if any)
02806     int m_expires;                       // Registration interval for protocols supporting it
02807     bool m_connected;                    // Logged in/out flag
02808 
02809 protected:
02810     // Remove from owner. Release data
02811     virtual void destroyed();
02812     // Method used by the contact to append itself to this account's list
02813     virtual void appendContact(ClientContact* contact);
02814     // Set ID and URI
02815     inline void setIdUri(const char* proto, const char* user,
02816         const char* host) {
02817             buildAccountId(m_id,proto,user,host);
02818             m_uri = String(user) + "@" + host;
02819         }
02820 
02821     URI m_id;                            // The account's id
02822     URI m_uri;                           // Account's URI
02823     ClientResource* m_resource;          // Account's resource
02824     ObjList m_contacts;                  // Account's contacts
02825 };
02826 
02831 class YATE_API ClientAccountList : public String, public Mutex
02832 {
02833 public:
02838     inline ClientAccountList(const char* name)
02839         : String(name), Mutex(true,"ClientAccountList")
02840         {}
02841 
02846     inline ObjList& accounts()
02847         { return m_accounts; }
02848 
02855     virtual ClientAccount* findAccount(const String& id, bool ref = false);
02856 
02864     virtual ClientContact* findContact(const String& account, const String& id, bool ref = false);
02865 
02872     virtual ClientContact* findContact(const String& builtId, bool ref = false);
02873 
02879     virtual bool appendAccount(ClientAccount* account);
02880 
02885     virtual void removeAccount(const String& id);
02886 
02887 protected:
02888     ObjList m_accounts;
02889 };
02890 
02896 class YATE_API ClientContact : public RefObject
02897 {
02898     friend class ClientAccount;
02899 public:
02907     ClientContact(ClientAccount* owner, const char* id, const char* name = 0,
02908         bool chat = false);
02909 
02917     ClientContact(ClientAccount* owner, NamedList& params, bool chat);
02918 
02923     inline ClientAccount* account()
02924         { return m_owner; }
02925 
02930     inline const URI& uri() const
02931         { return m_uri; }
02932 
02937     inline ObjList& resources()
02938         { return m_resources; }
02939 
02944     inline ObjList& groups()
02945         { return m_groups; }
02946 
02951     virtual const String& toString() const
02952         { return m_id; }
02953 
02958     inline void buildContactId(String& dest)
02959         {  buildContactId(dest,m_owner ? m_owner->toString() : String::empty(),m_id); }
02960 
02966     inline bool isChatWnd(Window* wnd)
02967         { return wnd && wnd->toString() == m_chatWndName; }
02968 
02973     inline bool hasChat()
02974         { return Client::self() && Client::self()->getWindow(m_chatWndName); }
02975 
02980     inline bool isChatVisible()
02981         { return Client::self() && Client::self()->getVisible(m_chatWndName); }
02982 
02988     inline bool showChat(bool active)
02989         { return Client::self() ? Client::self()->setVisible(m_chatWndName,active) : false; }
02990 
02995     inline Window* getChatWnd() const
02996         { return Client::self() ? Client::self()->getWindow(m_chatWndName) : 0; }
02997 
03003     void createChatWindow(bool force = false, const char* name = "chat");
03004 
03008     inline void destroyChatWindow() {
03009             if (m_chatWndName && Client::self())
03010                 Client::self()->closeWindow(m_chatWndName,false);
03011         }
03012 
03018     virtual String* findGroup(const String& group);
03019 
03025     virtual bool appendGroup(const String& group);
03026 
03032     virtual bool removeGroup(const String& group);
03033 
03040     virtual ClientResource* findResource(const String& id, bool ref = false);
03041 
03047     virtual ClientResource* findAudioResource(bool ref = false);
03048 
03054     virtual ClientResource* appendResource(const String& id);
03055 
03061     virtual bool removeResource(const String& id);
03062 
03068     static inline bool isChatWndPrefix(Window* wnd)
03069         { return wnd && wnd->toString().startsWith(s_chatPrefix); }
03070 
03077     static inline void buildContactId(String& dest, const String& account,
03078         const String& contact)
03079         { dest << String(account).toLower() << "|" << String(contact).toLower(); }
03080 
03087     static inline void splitContactId(const String& src, String& account,
03088         String& contact) {
03089             int pos = src.find('|');
03090             if (pos < 1) {
03091                 account = src;
03092                 return;
03093             }
03094             account = src.substr(0,pos);
03095             contact = src.substr(pos + 1);
03096         }
03097 
03098     // Chat window prefix
03099     static String s_chatPrefix;
03100 
03101     String m_name;                       // Contact's display name
03102     String m_subscription;               // Presence subscription state
03103 
03104 protected:
03105     // Remove from owner. Destroy the chat window. Release data
03106     virtual void destroyed();
03107 
03108     ClientAccount* m_owner;              // The account owning this contact
03109     String m_id;                         // The contact's id
03110     URI m_uri;                           // The contact's URI
03111     ObjList m_resources;                 // The contact's resource list
03112     ObjList m_groups;                    // The group(s) this contract belongs to 
03113 
03114 private:
03115     String m_chatWndName;                // Chat window name if any
03116 };
03117 
03122 class YATE_API ClientResource : public RefObject
03123 {
03124 public:
03131     inline ClientResource(const char* id, const char* name = 0, bool audio = true)
03132         : m_name(name ? name : id), m_audio(audio), m_priority(0), m_id(id)
03133         {}
03134 
03139     virtual const String& toString() const
03140         { return m_id; }
03141 
03142     String m_name;                       // Account's display name
03143     bool m_audio;                        // Audio capability flag
03144     int m_priority;                      // Resource priority
03145     String m_status;                     // Resource status string
03146 
03147 protected:
03148     String m_id;                         // The account's id
03149 };
03150 
03156 class YATE_API DurationUpdate : public RefObject
03157 {
03158 public:
03167     inline DurationUpdate(ClientLogic* logic, bool owner, const char* id,
03168         const char* name, unsigned int start = Time::secNow())
03169         : m_id(id), m_logic(0), m_name(name),   m_startTime(start)
03170         { setLogic(logic,owner); }
03171 
03175     virtual ~DurationUpdate();
03176 
03181     virtual const String& toString() const;
03182 
03188     void setLogic(ClientLogic* logic = 0, bool owner = true);
03189 
03199     virtual unsigned int update(unsigned int secNow, const String* table = 0,
03200         Window* wnd = 0, Window* skip = 0, bool force = false);
03201 
03209     virtual unsigned int buildTimeParam(NamedList& dest, unsigned int secNow,
03210         bool force = false);
03211 
03219     virtual unsigned int buildTimeString(String& dest, unsigned int secNow,
03220         bool force = false);
03221 
03231     static unsigned int buildTimeParam(NamedList& dest, const char* param, unsigned int secStart,
03232         unsigned int secNow, bool force = false);
03233 
03242     static unsigned int buildTimeString(String& dest, unsigned int secStart, unsigned int secNow,
03243         bool force = false);
03244 
03245 protected:
03249     virtual void destroyed();
03250 
03251     String m_id;                         // Duration's id
03252     ClientLogic* m_logic;                // Client logic having this object in its list
03253     String m_name;                       // Widget/column name
03254     unsigned int m_startTime;            // Start time
03255 };
03256 
03261 class YATE_API ClientSound : public String
03262 {
03263 public:
03270     inline ClientSound(const char* name, const char* file, const char* device = 0)
03271         : String(name), m_file(file), m_device(device), m_repeat(0),
03272         m_started(false), m_stereo(false)
03273         {}
03274 
03278     virtual ~ClientSound()
03279         { stop(); }
03280 
03284     virtual void destruct() {
03285             stop();
03286             String::destruct();
03287         }
03288 
03293     inline bool started() const
03294         { return m_started; }
03295 
03300     inline const String& device() const
03301         { return m_device; }
03302 
03307     inline void device(const char* dev)
03308         { Lock lock(s_soundsMutex); m_device = dev; }
03309 
03314     inline const String& file() const
03315         { return m_file; }
03316 
03323     inline void file(const char* filename, bool stereo)
03324         { Lock lock(s_soundsMutex); m_file = filename; m_stereo = stereo; }
03325 
03331     inline void setRepeat(unsigned int count)
03332         { m_repeat = count; }
03333 
03338     inline bool stereo() const
03339         { return m_stereo; }
03340 
03346     bool start(bool force = true);
03347 
03351     void stop();
03352 
03358     void setChannel(const String& chan, bool ok);
03359 
03365     bool attachSource(ClientChannel* chan);
03366 
03378     static bool build(const String& id, const char* file, const char* device = 0,
03379         unsigned int repeat = 0, bool resetExisting = true, bool stereo = false);
03380 
03386     static bool started(const String& name);
03387 
03394     static bool start(const String& name, bool force = true);
03395 
03400     static void stop(const String& name);
03401 
03408     static ClientSound* find(const String& token, bool byName = true);
03409 
03413     static ObjList s_sounds;
03414 
03418     static Mutex s_soundsMutex;
03419 
03424     static String s_calltoPrefix;
03425 
03426 protected:
03427     virtual bool doStart();
03428     virtual void doStop();
03429 
03430     String m_file;
03431     String m_device;
03432     unsigned int m_repeat;
03433     bool m_started;
03434     bool m_stereo;
03435     String m_channel;                    // Utility channel using this sound
03436 };
03437 
03438 }; // namespace TelEngine
03439 
03440 #endif /* __YATECBASE_H */
03441 
03442 /* vi: set ts=8 sw=4 sts=4 noet: */