1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/*
* MXit Protocol libPurple Plugin
*
* -- splash screens --
*
* Andrew Victor <libpurple@mxit.com>
*
* (C) Copyright 2009 MXit Lifestyle (Pty) Ltd.
* <http://www.mxitlifestyle.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "internal.h"
#include "purple.h"
#include "imgstore.h"
#include "protocol.h"
#include "mxit.h"
#include "splashscreen.h"
/*------------------------------------------------------------------------
* Return the ID of the current splash-screen.
*
* @param session The MXit session object
* @return The ID of the splash-screen (or NULL if no splash-screen)
*/
const char* splash_current(struct MXitSession* session)
{
const char* splashId = purple_account_get_string(session->acc, MXIT_CONFIG_SPLASHID, NULL);
if ((splashId != NULL) && (*splashId != '\0')) {
purple_debug_info(MXIT_PLUGIN_ID, "Current splashId: '%s'\n", splashId);
return splashId;
}
else
return NULL;
}
/*------------------------------------------------------------------------
* Indicate if splash-screen popups are enabled.
*
* @param session The MXit session object
* @return TRUE if the popup is enabled.
*/
gboolean splash_popup_enabled(struct MXitSession* session)
{
return purple_account_get_bool(session->acc, MXIT_CONFIG_SPLASHPOPUP, DEFAULT_SPLASH_POPUP);
}
/*------------------------------------------------------------------------
* Return if the current splash-screen is clickable.
*
* @param session The MXit session object
* @return TRUE or FALSE
*/
static gboolean splash_clickable(struct MXitSession* session)
{
return purple_account_get_bool(session->acc, MXIT_CONFIG_SPLASHCLICK, FALSE);
}
/*------------------------------------------------------------------------
* Remove the stored splash-screen (if it exists).
*
* @param session The MXit session object
*/
void splash_remove(struct MXitSession* session)
{
const char* splashId = NULL;
char* filename;
/* Get current splash ID */
splashId = splash_current(session);
if (splashId != NULL) {
purple_debug_info(MXIT_PLUGIN_ID, "Removing splashId: '%s'\n", splashId);
/* Delete stored splash image */
filename = g_strdup_printf("%s/mxit/%s.png", purple_user_dir(), splashId);
g_unlink(filename);
g_free(filename);
/* Clear current splash ID from settings */
purple_account_set_string(session->acc, MXIT_CONFIG_SPLASHID, "");
purple_account_set_bool(session->acc, MXIT_CONFIG_SPLASHCLICK, FALSE);
}
}
/*------------------------------------------------------------------------
* Save a new splash-screen for later display.
*
* @param session The MXit session object
* @param splashID The ID of the splash-screen
* @param data Splash-screen image data (PNG format)
* @param datalen Splash-screen image data size
*/
void splash_update(struct MXitSession* session, const char* splashId, const char* data, int datalen, gboolean clickable)
{
char* dir;
char* filename;
/* Remove the current splash-screen */
splash_remove(session);
/* Save the new splash image */
dir = g_strdup_printf("%s/mxit", purple_user_dir());
purple_build_dir(dir, S_IRUSR | S_IWUSR | S_IXUSR); /* ensure directory exists */
filename = g_strdup_printf("%s/%s.png", dir, splashId);
if (purple_util_write_data_to_file_absolute(filename, data, datalen)) {
/* Store new splash-screen ID to settings */
purple_account_set_string(session->acc, MXIT_CONFIG_SPLASHID, splashId);
purple_account_set_bool(session->acc, MXIT_CONFIG_SPLASHCLICK, clickable );
}
g_free(dir);
g_free(filename);
}
/*------------------------------------------------------------------------
* The user has clicked OK on the Splash request form.
*
* @param gc The connection object
* @param fields The list of fields in the accepted form
*/
static void splash_click_ok(PurpleConnection* gc, PurpleRequestFields* fields)
{
struct MXitSession* session = (struct MXitSession*) gc->proto_data;
const char* splashId;
/* Get current splash ID */
splashId = splash_current(session);
if (splashId == NULL) /* no splash-screen */
return;
/* if is clickable, then send click event */
if (splash_clickable(session))
mxit_send_splashclick(session, splashId);
}
/*------------------------------------------------------------------------
* Display the current splash-screen.
*
* @param session The MXit session object
*/
void splash_display(struct MXitSession* session)
{
const char* splashId = NULL;
char* filename;
gchar* imgdata;
gsize imglen;
int imgid = -1;
/* Get current splash ID */
splashId = splash_current(session);
if (splashId == NULL) /* no splash-screen */
return;
purple_debug_info(MXIT_PLUGIN_ID, "Display Splash: '%s'\n", splashId);
/* Load splash-screen image from file */
filename = g_strdup_printf("%s/mxit/%s.png", purple_user_dir(), splashId);
if (g_file_get_contents(filename, &imgdata, &imglen, NULL)) {
char buf[128];
/* Add splash-image to imagestore */
imgid = purple_imgstore_add_with_id(g_memdup(imgdata, imglen), imglen, NULL);
/* Generate and display message */
g_snprintf(buf, sizeof(buf), "<img id=\"%d\">", imgid);
/* Open a request-type popup to display the image */
{
PurpleRequestFields* fields;
PurpleRequestFieldGroup* group;
PurpleRequestField* field;
fields = purple_request_fields_new();
group = purple_request_field_group_new(NULL);
purple_request_fields_add_group(fields, group);
field = purple_request_field_image_new("splash", "", imgdata, imglen); /* add splash image */
purple_request_field_group_add_field(group, field);
if (splash_clickable(session)) {
purple_request_fields(session->con, _("MXit Advertising"), NULL, NULL, fields,
_("More Information"), G_CALLBACK(splash_click_ok), _("Close"), NULL, session->acc, NULL, NULL, session->con);
}
else {
purple_request_fields(session->con, _("MXit Advertising"), NULL, NULL, fields,
_("Continue"), G_CALLBACK(splash_click_ok), _("Close"), NULL, session->acc, NULL, NULL, session->con);
}
}
/* Release reference to image */
purple_imgstore_unref_by_id(imgid);
g_free(imgdata);
}
g_free(filename);
}
|