Warning, file /libraries/snorenotify/src/plugins/backends/snarl/SnarlInterface.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 #ifndef SNARL_INTERFACE_V42_H
0002 #define SNARL_INTERFACE_V42_H
0003 
0004 #if defined(__MINGW32__) && !defined(MINGW_HAS_SECURE_API)
0005 #define MINGW_HAS_SECURE_API 1
0006 #endif
0007 
0008 #include <tchar.h>
0009 #include <windows.h>
0010 #include <cstdio>
0011 #include <vector>
0012 #include <sstream>
0013 
0014 #ifndef SMTO_NOTIMEOUTIFNOTHUNG
0015 #define SMTO_NOTIMEOUTIFNOTHUNG 8
0016 #endif
0017 
0018 namespace Snarl
0019 {
0020 namespace V42
0021 {
0022 
0023 static LPCTSTR SnarlWindowClass = _T("w>Snarl");
0024 static LPCTSTR SnarlWindowTitle = _T("Snarl");
0025 
0026 static LPCTSTR SnarlGlobalMsg = _T("SnarlGlobalEvent");
0027 static LPCTSTR SnarlAppMsg    = _T("SnarlAppMessage");
0028 
0029 static const DWORD WM_SNARLTEST = WM_USER + 237;
0030 
0031 /// <summary>Application requests - these values appear in wParam.<para>Application should launch its settings UI</para></summary>
0032 static const WPARAM AppDoPrefs = 1;
0033 /// <summary>Application requests - these values appear in wParam.<para>Application should show its About... dialog</para></summary>
0034 static const WPARAM AppDoAbout = 2;
0035 
0036 // Enums put in own namespace, because ANSI C++ doesn't decorate enums with tagname :(
0037 namespace SnarlEnums
0038 {
0039 
0040 /// <summary>
0041 /// Global event identifiers - sent as Windows broadcast messages.
0042 /// These values appear in wParam of the message.
0043 /// </summary>
0044 enum GlobalEvent {
0045     SnarlLaunched = 1,      // Snarl has just started running
0046     SnarlQuit = 2,          // Snarl is about to stop running
0047     SnarlGlobalStopped = 3, // Sent when stopped by user - Also sent to registered window
0048     SnarlGlobalStarted = 4, // Sent when started by user - Also sent to registered window
0049 };
0050 
0051 enum SnarlStatus {
0052     Success = 0,
0053 
0054     // Snarl-Stopped/Started/UserAway/UserBack is defined in the GlobalEvent struct in VB6 code,
0055     // but are sent directly to a registered window, so in C# they are defined here instead.
0056     // Implemented as of Snarl R2.4 Beta3
0057     SnarlStopped = 3,              // Sent when stopped by user - Also sent as broadcast message
0058     SnarlStarted,                  // Sent when started by user - Also sent as broadcast message
0059     SnarlUserAway,                 // Away mode was enabled
0060     SnarlUserBack,                 // Away mode was disabled
0061 
0062     // Win32 callbacks (renamed under V42)
0063     CallbackRightClick = 32,       // Deprecated as of V42, ex. SNARL_NOTIFICATION_CLICKED/SNARL_NOTIFICATION_CANCELLED
0064     CallbackTimedOut,
0065     CallbackInvoked,               // left clicked and no default callback assigned
0066     CallbackMenuSelected,          // HIWORD(wParam) contains 1-based menu item index
0067     CallbackMiddleClick,           // Deprecated as of V42
0068     CallbackClosed,
0069 
0070     // critical errors
0071     ErrorFailed = 101,             // miscellaneous failure
0072     ErrorUnknownCommand,           // specified command not recognised
0073     ErrorTimedOut,                 // Snarl took too long to respond
0074     //104 gen critical #4
0075     //105 gen critical #5
0076     ErrorBadSocket = 106,          // invalid socket (or some other socket-related error)
0077     ErrorBadPacket = 107,          // badly formed request
0078     ErrorInvalidArg = 108,         // arg supplied was invalid (Added in v42.56)
0079     ErrorArgMissing = 109,         // required argument missing
0080     ErrorSystem,                   // internal system error
0081     //120 libsnarl critical block
0082     ErrorAccessDenied = 121,       // libsnarl only
0083     //130 SNP/3.0-specific
0084     ErrorUnsupportedVersion = 131, // requested SNP version is not supported
0085     ErrorNoActionsProvided,        // empty request
0086     ErrorUnsupportedEncryption,    // requested encryption type is not supported
0087     ErrorUnsupportedHashing,       // requested message hashing type is not supported
0088 
0089     // warnings
0090     ErrorNotRunning = 201,         // Snarl handling window not found
0091     ErrorNotRegistered,
0092     ErrorAlreadyRegistered,        // not used yet; sn41RegisterApp() returns existing token
0093     ErrorClassAlreadyExists,       // not used yet
0094     ErrorClassBlocked,
0095     ErrorClassNotFound,
0096     ErrorNotificationNotFound,
0097     ErrorFlooding,                 // notification generated by same class within quantum
0098     ErrorDoNotDisturb,             // DnD mode is in effect was not logged as missed
0099     ErrorCouldNotDisplay,          // not enough space on-screen to display notification
0100     ErrorAuthFailure,              // password mismatch
0101     // Release 2.4.2
0102     ErrorDiscarded,                // discarded for some reason, e.g. foreground app match
0103     ErrorNotSubscribed,            // subscriber not found
0104 
0105     // informational
0106     // code 250 reserved for future use
0107     WasMerged = 251,               // notification was merged, returned token is the one we merged with
0108 
0109     // callbacks
0110     // code 300 reserved for future use
0111     NotifyGone = 301,              // reserved for future use
0112 
0113     // The following are currently specific to SNP 2.0 and are effectively the
0114     // Win32 SNARL_CALLBACK_nnn constants with 270 added to them
0115 
0116     // SNARL_NOTIFY_CLICK = 302    // indicates notification was right-clicked (deprecated as of V42)
0117     NotifyExpired = 303,
0118     NotifyInvoked = 304,           // note this was "ACK" in a previous life
0119     NotifyMenu,                    // indicates an item was selected from user-defined menu (deprecated as of V42)
0120     // SNARL_NOTIFY_EX_CLICK       // user clicked the middle mouse button (deprecated as of V42)
0121     NotifyClosed = 307,            // // user clicked the notification's close gadget (GNTP only)
0122 
0123     // the following is generic to SNP and the Win32 API
0124     NotifyAction = 308,             // user picked an action from the list, the data value will indicate which one
0125 
0126     // C++ interface custom errors- not part of official API!
0127     ErrorCppInterface = 1001
0128 };
0129 
0130 /// <summary>
0131 /// The priority of messages.
0132 /// See <cref>http://sourceforge.net/apps/mediawiki/snarlwin/index.php?title=Generic_API#notify</cref>
0133 /// </summary>
0134 enum MessagePriority {
0135     PriorityUndefined = -2,
0136     PriorityLow = -1,
0137     PriorityNormal = 0,
0138     PriorityHigh = 1
0139 };
0140 
0141 /// <summary>
0142 /// Application flags - features this app supports.
0143 /// </summary>
0144 enum AppFlags {
0145     AppFlagNone = 0,
0146     AppHasPrefs = 1,
0147     AppHasAbout = 2,
0148     AppIsWindowless = 0x8000
0149 };
0150 
0151 } // namespace SnarlEnums
0152 
0153 template<class T>
0154 class SnarlParameterList
0155 {
0156 public:
0157     typedef std::pair<std::basic_string<T>, std::basic_string<T> > PairType;
0158 
0159     SnarlParameterList()
0160     {
0161     }
0162 
0163     explicit SnarlParameterList(int initialCapacity)
0164     {
0165         list.reserve(initialCapacity);
0166     }
0167 
0168     void Add(const T *_key, const T *_value)
0169     {
0170         if (_value != NULL) {
0171             list.push_back(PairType(std::basic_string<T> (_key), std::basic_string<T> (_value)));           //
0172         }
0173     }
0174 
0175     void Add(const T *_key, LONG32 _value)
0176     {
0177         std::basic_stringstream<T> valStr;
0178         valStr << _value;
0179         list.push_back(PairType(std::basic_string<T> (_key), valStr.str()));
0180     }
0181 
0182     void Add(const T *_key, void *_value)
0183     {
0184         if (_value != NULL) {
0185             std::basic_stringstream<T> valStr;
0186             valStr << (INT_PTR) _value;   // Uckly hack, to get stringstream to print void* as decimal not hex
0187 
0188             list.push_back(PairType(std::basic_string<T> (_key), valStr.str()));
0189         }
0190     }
0191 
0192     const std::vector<PairType> &GetList() const
0193     {
0194         return list;
0195     }
0196 
0197 private:
0198     std::vector<PairType> list;
0199 };
0200 
0201 // ----------------------------------------------------------------------------------------
0202 // SnarlInterface class definition
0203 // ----------------------------------------------------------------------------------------
0204 class SnarlInterface
0205 {
0206 public:
0207     /// <summary>Requests strings known by Snarl</summary>
0208     class Requests
0209     {
0210     public:
0211         static LPCSTR  AddActionA()
0212         {
0213             return  "addaction";
0214         }
0215         static LPCWSTR AddActionW()
0216         {
0217             return L"addaction";
0218         }
0219         static LPCSTR  AddClassA()
0220         {
0221             return  "addclass";
0222         }
0223         static LPCWSTR AddClassW()
0224         {
0225             return L"addclass";
0226         }
0227         static LPCSTR  ClearActionsA()
0228         {
0229             return  "clearactions";
0230         }
0231         static LPCWSTR ClearActionsW()
0232         {
0233             return L"clearactions";
0234         }
0235         static LPCSTR  ClearClassesA()
0236         {
0237             return  "clearclasses";
0238         }
0239         static LPCWSTR ClearClassesW()
0240         {
0241             return L"clearclasses";
0242         }
0243         static LPCSTR  HelloA()
0244         {
0245             return  "hello";
0246         }
0247         static LPCWSTR HelloW()
0248         {
0249             return L"hello";
0250         }
0251         static LPCSTR  HideA()
0252         {
0253             return  "hide";
0254         }
0255         static LPCWSTR HideW()
0256         {
0257             return L"hide";
0258         }
0259         static LPCSTR  IsVisibleA()
0260         {
0261             return  "isvisible";
0262         }
0263         static LPCWSTR IsVisibleW()
0264         {
0265             return L"isvisible";
0266         }
0267         static LPCSTR  NotifyA()
0268         {
0269             return  "notify";
0270         }
0271         static LPCWSTR NotifyW()
0272         {
0273             return L"notify";
0274         }
0275         static LPCSTR  RegisterA()
0276         {
0277             return  "reg";    // register
0278         }
0279         static LPCWSTR RegisterW()
0280         {
0281             return L"reg";
0282         }
0283         static LPCSTR  RemoveClassA()
0284         {
0285             return  "remclass";
0286         }
0287         static LPCWSTR RemoveClassW()
0288         {
0289             return L"remclass";
0290         }
0291         static LPCSTR  UnregisterA()
0292         {
0293             return  "unregister";
0294         }
0295         static LPCWSTR UnregisterW()
0296         {
0297             return L"unregister";
0298         }
0299         static LPCSTR  UpdateAppA()
0300         {
0301             return  "updateapp";
0302         }
0303         static LPCWSTR UpdateAppW()
0304         {
0305             return L"updateapp";
0306         }
0307         static LPCSTR  UpdateA()
0308         {
0309             return  "update";
0310         }
0311         static LPCWSTR UpdateW()
0312         {
0313             return L"update";
0314         }
0315         static LPCSTR  VersionA()
0316         {
0317             return  "version";
0318         }
0319         static LPCWSTR VersionW()
0320         {
0321             return L"version";
0322         }
0323     };
0324 
0325     SnarlInterface();
0326     virtual ~SnarlInterface();
0327 
0328     // ------------------------------------------------------------------------------------
0329     // Static functions
0330     // ------------------------------------------------------------------------------------
0331 
0332     // Use FreeString, when SnarlInterface returns a null terminated string pointer
0333     static LPTSTR AllocateString(size_t n)
0334     {
0335         return new TCHAR[n];
0336     }
0337     static void FreeString(LPSTR str)
0338     {
0339         delete [] str;
0340         str = NULL;
0341     }
0342     static void FreeString(LPCSTR str)
0343     {
0344         delete [] str;
0345     }
0346     static void FreeString(LPWSTR str)
0347     {
0348         delete [] str;
0349         str = NULL;
0350     }
0351     static void FreeString(LPCWSTR str)
0352     {
0353         delete [] str;
0354     }
0355 
0356     /// <summary>Send message to Snarl.</summary>
0357     /// <param name='request'>The request string. If using unicode version, the string will be UTF8 encoded before sending.</param>
0358     /// <param name='replyTimeout'>Time to wait before timeout - Default = 1000</param>
0359     /// <returns>
0360     ///   Return zero or positive on Success.
0361     ///   Negative on failure. (Get error code by abs(return_value))
0362     ///   <see>http://sourceforge.net/apps/mediawiki/snarlwin/index.php?title=Windows_API#Return_Value</see>
0363     /// </returns>
0364     static LONG32 DoRequest(LPCSTR request, UINT replyTimeout = 1000);
0365     static LONG32 DoRequest(LPCWSTR request, UINT replyTimeout = 1000);
0366 
0367     /// <summary>Escapes a string, so it can be passed to Snarl.</summary>
0368     /// <remarks>
0369     ///   Should only be used, if you are using DoRequest() and not the helper functions.
0370     ///   Remember to Escape each key/value pair individually.
0371     /// </remarks>
0372     static std::basic_string<char> &Escape(std::basic_string<char> &str);
0373     static std::basic_string<wchar_t> &Escape(std::basic_string<wchar_t> &str);
0374 
0375     /// <summary>Returns the global Snarl Application message  (V39)</summary>
0376     /// <returns>Returns Snarl application registered message.</returns>
0377     static UINT AppMsg();
0378 
0379     /// <summary>
0380     ///     Returns the value of Snarl's global registered message.
0381     ///     Notes:
0382     ///       Snarl registers SNARL_GLOBAL_MSG during startup which it then uses to communicate
0383     ///       with all running applications through a Windows broadcast message. This function can
0384     ///       only fail if for some reason the Windows RegisterWindowMessage() function fails
0385     ///       - given this, this function *cannnot* be used to test for the presence of Snarl.
0386     /// </summary>
0387     /// <returns>A 16-bit value (translated to 32-bit) which is the registered Windows message for Snarl.</returns>
0388     static UINT Broadcast();
0389 
0390     /// <summary>
0391     ///     Get the path to where Snarl is installed.
0392     ///     ** Remember to call <see cref="FreeString(LPSTR)" /> on the returned string !!!
0393     /// </summary>
0394     /// <returns>Returns the path to where Snarl is installed.</returns>
0395     /// <remarks>This is a V39 API method.</remarks>
0396     static LPCTSTR GetAppPath();
0397 
0398     /// <summary>
0399     ///     Get the path to where the default Snarl icons are located.
0400     ///     <para>** Remember to call <see cref="FreeString(LPSTR)" /> on the returned string !!!</para>
0401     /// </summary>
0402     /// <returns>Returns the path to where the default Snarl icons are located.</returns>
0403     /// <remarks>This is a V39 API method.</remarks>
0404     static LPCTSTR GetIconsPath();
0405 
0406     /// <summary>Returns a handle to the Snarl Dispatcher window  (V37)</summary>
0407     /// <returns>Returns handle to Snarl Dispatcher window, or zero if it's not found.</returns>
0408     /// <remarks>This is now the preferred way to test if Snarl is actually running.</remarks>
0409     static HWND GetSnarlWindow();
0410 
0411     /// <summary>Get Snarl version, if it is running.</summary>
0412     /// <returns>Returns a number indicating Snarl version.</returns>
0413     static LONG32 GetVersion();
0414 
0415     /// <summary>Check whether Snarl is running</summary>
0416     /// <returns>Returns true if Snarl system was found running.</returns>
0417     static BOOL IsSnarlRunning();
0418 
0419     // ------------------------------------------------------------------------------------
0420 
0421     /// <summary>Adds an action to an existing (on-screen or in the missed list) notification.</summary>
0422     LONG32 AddAction(LONG32 msgToken, LPCSTR  label, LPCSTR  cmd);
0423     LONG32 AddAction(LONG32 msgToken, LPCWSTR label, LPCWSTR cmd);
0424 
0425     /// <summary>Add a notification class to Snarl.</summary>
0426     LONG32 AddClass(LPCSTR classId, LPCSTR name, LPCSTR title = NULL, LPCSTR text = NULL, LPCSTR icon = NULL, LPCSTR sound = NULL, LONG32 duration = NULL, LPCSTR callback = NULL, bool enabled = true);
0427     LONG32 AddClass(LPCWSTR classId, LPCWSTR name, LPCWSTR title = NULL, LPCWSTR text = NULL, LPCWSTR icon = NULL, LPCWSTR sound = NULL, LONG32 duration = -1, LPCWSTR callback = NULL, bool enabled = true);
0428 
0429     /// <summary>Remove all notification classes in one call.</summary>
0430     LONG32 ClearActions(LONG32 msgToken);
0431 
0432     /// <summary>Remove all notification classes in one call.</summary>
0433     LONG32 ClearClasses();
0434 
0435     /// <summary>GetLastMsgToken() returns token of the last message sent to Snarl.</summary>
0436     /// <returns>Returns message token of last message.</returns>
0437     /// <remarks>This function is not in the official API!</remarks>
0438     LONG32 GetLastMsgToken() const;
0439 
0440     /// <summary>Hide a Snarl notification.</summary>
0441     LONG32 Hide(LONG32 msgToken);
0442 
0443     /// <summary>Test if a Snarl notification is visible. If the message is visible the function returns SnarlEnums::Success.</summary>
0444     LONG32 IsVisible(LONG32 msgToken);
0445 
0446     /// <summary>Show a Snarl notification.</summary>
0447     /// <returns>Returns the notification token or negative on failure.</returns>
0448     /// <remarks>You can use <see cref="GetLastMsgToken()" /> to get the last token.</remarks>
0449     LONG32 Notify(LPCSTR classId = NULL, LPCSTR title = NULL, LPCSTR text = NULL, LONG32 timeout = -1, LPCSTR iconPath = NULL, LPCSTR iconBase64 = NULL, SnarlEnums::MessagePriority priority = SnarlEnums::PriorityUndefined, LPCSTR uid = NULL, LPCSTR callback = NULL, LPCSTR value = NULL);
0450     LONG32 Notify(LPCWSTR classId = NULL, LPCWSTR title = NULL, LPCWSTR text = NULL, LONG32 timeout = -1, LPCWSTR iconPath = NULL, LPCWSTR iconBase64 = NULL,  SnarlEnums::MessagePriority priority = SnarlEnums::PriorityUndefined, LPCWSTR uid = NULL, LPCWSTR callback = NULL, LPCWSTR value = NULL);
0451 
0452     /// <summary>Register application with Snarl.</summary>
0453     /// <returns>The application token or negative on failure.</returns>
0454     /// <remarks>The application token is saved in SnarlInterface member variable, so just use return value to check for error.</remarks>
0455     LONG32 Register(LPCSTR  signature, LPCSTR  title, LPCSTR  icon = NULL, LPCSTR  password = NULL, HWND hWndReplyTo = NULL, LONG32 msgReply = 0, SnarlEnums::AppFlags flags = SnarlEnums::AppFlagNone);
0456     LONG32 Register(LPCWSTR signature, LPCWSTR title, LPCWSTR icon = NULL, LPCWSTR password = NULL, HWND hWndReplyTo = NULL, LONG32 msgReply = 0, SnarlEnums::AppFlags flags = SnarlEnums::AppFlagNone);
0457 
0458     /// <summary>Remove a notification class added with AddClass().</summary>
0459     LONG32 RemoveClass(LPCSTR classId);
0460     LONG32 RemoveClass(LPCWSTR classId);
0461 
0462     /// <summary>Update the text or other parameters of a visible Snarl notification.</summary>
0463     LONG32 Update(LONG32 msgToken, LPCSTR classId = NULL, LPCSTR title = NULL, LPCSTR text = NULL, LONG32 timeout = -1, LPCSTR iconPath = NULL, LPCSTR iconBase64 = NULL, SnarlEnums::MessagePriority priority = SnarlEnums::PriorityUndefined, LPCSTR callback = NULL, LPCSTR value = NULL);
0464     LONG32 Update(LONG32 msgToken, LPCWSTR classId = NULL, LPCWSTR title = NULL, LPCWSTR text = NULL, LONG32 timeout = -1, LPCWSTR iconPath = NULL, LPCWSTR iconBase64 = NULL, SnarlEnums::MessagePriority priority = SnarlEnums::PriorityUndefined, LPCWSTR callback = NULL, LPCWSTR value = NULL);
0465 
0466     /// <summary>Unregister application with Snarl when application is closing.</summary>
0467     LONG32 Unregister(LPCSTR signature);
0468     LONG32 Unregister(LPCWSTR signature);
0469 
0470     /// <summary>Update information provided when calling RegisterApp.</summary>
0471     /*LONG32 UpdateApp(LPCSTR title = NULL, LPCSTR icon = NULL);
0472     LONG32 UpdateApp(LPCWSTR title = NULL, LPCWSTR icon = NULL);*/
0473 
0474 private:
0475 
0476     /// <summary>Convert a unicode string to UTF8</summary>
0477     /// <returns>Returns pointer to the new string - Remember to delete [] returned string !</returns>
0478     /// <remarks>Remember to call FreeString on returned string !!!</remarks>
0479     static LPSTR  WideToUTF8(LPCWSTR szWideStr);
0480 
0481     static LONG32 DoRequest(LPCSTR request, SnarlParameterList<char> &spl, UINT replyTimeout = 1000);
0482     static LONG32 DoRequest(LPCWSTR request, SnarlParameterList<wchar_t> &spl, UINT replyTimeout = 1000);
0483 
0484     void SetPassword(LPCSTR password);
0485     void SetPassword(LPCWSTR password);
0486     void ClearPassword();
0487 
0488     LONG32 appToken;
0489     LONG32 lastMsgToken;
0490     LPSTR szPasswordA;
0491     LPWSTR szPasswordW;
0492 }; // class SnarlInterface
0493 
0494 } // namespace V42
0495 } // namespace Snarl
0496 
0497 #endif // SNARL_INTERFACE_V42_H
0498