Fix user info and clean up source code

This commit is contained in:
mjentsch 2015-01-21 23:49:42 +01:00
parent 58f34cbe8a
commit ac9ce29b1d
10 changed files with 321 additions and 185 deletions

View file

@ -15,7 +15,7 @@ OBJ=objs
LIB=libs
DIR_LIST=${DEP} ${AUTO} ${EXE} ${OBJ} ${LIB} ${DEP}/auto ${OBJ}/auto ${DEP}/lodepng ${OBJ}/lodepng
PLUGIN_OBJECTS=${OBJ}/tgp-net.o ${OBJ}/tgp-timers.o ${OBJ}/msglog.o ${OBJ}/telegram-base.o ${OBJ}/telegram-purple.o ${OBJ}/tgp-2prpl.o ${OBJ}/tgp-structs.o ${OBJ}/lodepng/lodepng.o
PLUGIN_OBJECTS=${OBJ}/tgp-net.o ${OBJ}/tgp-timers.o ${OBJ}/msglog.o ${OBJ}/telegram-base.o ${OBJ}/telegram-purple.o ${OBJ}/tgp-2prpl.o ${OBJ}/tgp-structs.o ${OBJ}/tgp-utils.o ${OBJ}/lodepng/lodepng.o
ALL_OBJS=${PLUGIN_OBJECTS}
.SUFFIXES:

View file

@ -11,6 +11,7 @@
C410949019BB2D7D0083BF3F /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = C410948E19BB2D7D0083BF3F /* InfoPlist.strings */; };
C410949B19BB337A0083BF3F /* TelegramPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = C410949A19BB337A0083BF3F /* TelegramPlugin.m */; };
C41D58411A16D88E00B22448 /* tgp-2prpl.c in Sources */ = {isa = PBXBuildFile; fileRef = C41D58401A16D88E00B22448 /* tgp-2prpl.c */; };
C425F9181A7069C300361AFC /* tgp-utils.c in Sources */ = {isa = PBXBuildFile; fileRef = C425F9161A7069C300361AFC /* tgp-utils.c */; };
C438CE271A12BEAF00E1DA0F /* telegram.png in Resources */ = {isa = PBXBuildFile; fileRef = C438CE231A12BEAF00E1DA0F /* telegram.png */; };
C438CE281A12BEAF00E1DA0F /* telegram16.png in Resources */ = {isa = PBXBuildFile; fileRef = C438CE241A12BEAF00E1DA0F /* telegram16.png */; };
C438CE291A12BEAF00E1DA0F /* telegram22.png in Resources */ = {isa = PBXBuildFile; fileRef = C438CE251A12BEAF00E1DA0F /* telegram22.png */; };
@ -51,6 +52,8 @@
C410949A19BB337A0083BF3F /* TelegramPlugin.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TelegramPlugin.m; sourceTree = "<group>"; };
C41D583F1A16D86A00B22448 /* tgp-2prpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "tgp-2prpl.h"; path = "../tgp-2prpl.h"; sourceTree = "<group>"; };
C41D58401A16D88E00B22448 /* tgp-2prpl.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = "tgp-2prpl.c"; path = "../tgp-2prpl.c"; sourceTree = "<group>"; };
C425F9161A7069C300361AFC /* tgp-utils.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = "tgp-utils.c"; path = "../tgp-utils.c"; sourceTree = "<group>"; };
C425F9171A7069C300361AFC /* tgp-utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "tgp-utils.h"; path = "../tgp-utils.h"; sourceTree = "<group>"; };
C438CE231A12BEAF00E1DA0F /* telegram.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = telegram.png; path = ../imgs/telegram.png; sourceTree = "<group>"; };
C438CE241A12BEAF00E1DA0F /* telegram16.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = telegram16.png; path = ../imgs/telegram16.png; sourceTree = "<group>"; };
C438CE251A12BEAF00E1DA0F /* telegram22.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = telegram22.png; path = ../imgs/telegram22.png; sourceTree = "<group>"; };
@ -197,6 +200,8 @@
C4BF990319BB8B200038D507 /* telegram-purple */ = {
isa = PBXGroup;
children = (
C425F9161A7069C300361AFC /* tgp-utils.c */,
C425F9171A7069C300361AFC /* tgp-utils.h */,
C4D819041A5C862E0044CBA9 /* tgp-structs.c */,
C4D819051A5C862E0044CBA9 /* tgp-structs.h */,
C438CE371A12C0C900E1DA0F /* msglog.h */,
@ -301,6 +306,7 @@
C410949B19BB337A0083BF3F /* TelegramPlugin.m in Sources */,
C4877C1E19BB676B006FA91F /* AdiumTelegramAccount.m in Sources */,
C41D58411A16D88E00B22448 /* tgp-2prpl.c in Sources */,
C425F9181A7069C300361AFC /* tgp-utils.c in Sources */,
C4D819031A5C85FE0044CBA9 /* lodepng.c in Sources */,
C4D819061A5C862E0044CBA9 /* tgp-structs.c in Sources */,
);

View file

@ -61,6 +61,7 @@
#include "telegram-base.h"
#include "telegram-purple.h"
#include "msglog.h"
#include "tgp-utils.h"
#define _(m) m
@ -77,42 +78,10 @@ static connection_data *get_conn_from_buddy (PurpleBuddy *buddy) {
return c;
}
static const char *format_time (time_t date) {
struct tm *datetime = localtime(&date);
return purple_utf8_strftime("%Y.%m.%d %H:%M:%S", datetime);
}
static char *format_status (struct tgl_user_status *status) {
return status->online ? "Online" : "Mobile";
}
static char *format_img_full (int imgstore) {
const char *br = "<br>";
// <br>'s look ugly in Adium, but no <br> will look ugly in Pidgin
#ifdef __ADIUM_
br = "";
#endif
return g_strdup_printf ("%s<img id=\"%u\">",br, imgstore);
}
static char *format_size (gint64 size) {
char *sizes[] = {
"b",
"Kb",
"Mb",
"Gb",
"Pb"
};
int base = 0;
double s = (double) size;
while (s > 1024 && base < 4) {
s /= 1024;
++ base;
}
return g_strdup_printf ("%.1f %s, ", s, sizes[base]);
}
static char *format_service_msg (struct tgl_state *TLS, struct tgl_message *M)
{
assert (M && M->service);
@ -200,6 +169,29 @@ static char *format_service_msg (struct tgl_state *TLS, struct tgl_message *M)
return txt;
}
char *format_user_status (struct tgl_user_status *status)
{
char *when;
switch (status->online) {
case -1:
when = g_strdup_printf("%s", format_time (status->when));
break;
case -2:
when = g_strdup_printf("recently");
break;
case -3:
when = g_strdup_printf("last week");
break;
case -4:
when = g_strdup_printf("last month");
break;
default:
when = g_strdup ("unknown");
break;
}
return when;
}
static char *format_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) {
const char *d[4];
d[0] = a1; d[1] = a2; d[2] = a3; d[3] = a4;
@ -235,7 +227,7 @@ static char *format_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const c
}
static char *format_document_desc (char *type, char *caption, gint64 size) {
char *s = format_size (size);
char *s = tgp_g_format_size (size);
char *msg = g_strdup_printf ("[%s] %s %s", type, caption, s);
g_free (s);
return msg;
@ -299,16 +291,6 @@ static tgl_peer_t *find_peer_by_name (struct tgl_state *TLS, const char *who) {
return NULL;
}
static int our_msg (struct tgl_state *TLS, struct tgl_message *M) {
//return tgl_get_peer_id(M->from_id) == TLS->our_id;
//return M->out;
return (M->flags & FLAG_SESSION_OUTBOUND) != 0;
}
static int out_msg (struct tgl_state *TLS, struct tgl_message *M) {
return M->out;
}
static gboolean queries_timerfunc (gpointer data) {
debug ("queries_timerfunc()\n");
connection_data *conn = data;
@ -378,14 +360,12 @@ void on_message_load_photo (struct tgl_state *TLS, void *extra, int success, cha
struct tgl_message *M = extra;
switch (tgl_get_peer_type (M->to_id)) {
case TGL_PEER_CHAT:
debug ("PEER_CHAT\n");
if (!our_msg(TLS, M)) {
chat_add_message (TLS, M, image);
}
break;
case TGL_PEER_USER:
debug ("PEER_USER\n");
if (out_msg(TLS, M)) {
p2tgl_got_im (TLS, M->to_id, image, PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_IMAGES, M->date);
} else {
@ -405,7 +385,6 @@ static void update_message_received (struct tgl_state *TLS, struct tgl_message *
conn->updated = 1;
if (M->service) {
debug ("service message, skipping...\n");
char *text = format_service_msg (TLS, M);
if (text) {
switch (tgl_get_peer_type (M->to_id)) {
@ -491,10 +470,7 @@ static void update_user_handler (struct tgl_state *TLS, struct tgl_user *user, u
p2tgl_buddy_update (TLS, (tgl_peer_t *)user, flags);
}
if (flags & TGL_UPDATE_PHOTO) {
get_user_info_data* info_data = malloc (sizeof(get_user_info_data));
info_data->show_info = 0;
info_data->peer = 0;
tgl_do_get_user_info (TLS, user->id, 0, on_user_get_info, info_data);
tgl_do_get_user_info (TLS, user->id, 0, on_user_get_info, get_user_info_data_new (0, user->id));
}
if (flags & TGL_UPDATE_DELETED && buddy) {
purple_blist_remove_buddy (buddy);
@ -504,6 +480,7 @@ static void update_user_handler (struct tgl_state *TLS, struct tgl_user *user, u
static void write_secret_chat_cb (struct tgl_state *TLS, void *extra, int success, struct tgl_secret_chat *E) {
debug ("update_secret_chat_handle success=%d", success);
if (!success) { return; }
write_secret_chat_file (TLS);
}
@ -612,15 +589,6 @@ static void update_user_typing (struct tgl_state *TLS, struct tgl_user *U, enum
}
}
PurpleNotifyUserInfo *create_user_notify_info(struct tgl_user *usr) {
PurpleNotifyUserInfo *info = purple_notify_user_info_new();
purple_notify_user_info_add_pair(info, "First name", usr->first_name);
purple_notify_user_info_add_pair(info, "Last name", usr->last_name);
purple_notify_user_info_add_pair(info, "Phone", usr->phone);
purple_notify_user_info_add_pair(info, "Status", usr->status.online == 1 ? "Online" : "Offline");
return info;
}
static void on_contact_added (struct tgl_state *TLS,void *callback_extra, int success, int size, struct tgl_user *users[]) {
PurpleBuddy *buddy = callback_extra;
@ -631,91 +599,52 @@ static void on_contact_added (struct tgl_state *TLS,void *callback_extra, int su
}
static void on_userpic_loaded (struct tgl_state *TLS, void *extra, int success, char *filename) {
if (!success) {
struct download_desc *dld = extra;
struct tgl_user *U = dld->data;
warning ("Can not load userpic for user %s %s\n", U->first_name, U->last_name);
}
connection_data *conn = TLS->ev_base;
gchar *data = NULL;
size_t len;
GError *err = NULL;
g_file_get_contents (filename, &data, &len, &err);
int imgStoreId = purple_imgstore_add_with_id (g_memdup(data, (guint)len), len, NULL);
used_images_add (conn, imgStoreId);
struct download_desc *dld = extra;
struct tgl_user *U = dld->data;
tgl_peer_t *P = tgl_peer_get (TLS, dld->get_user_info_data->peer);
if (imgStoreId <= 0) {
if (!success || !P) {
warning ("Can not load userpic for user %s %s\n", U->first_name, U->last_name);
goto fin;
}
int imgStoreId = p2tgl_imgstore_add_with_id (filename);
if (imgStoreId > 0) {
used_images_add (conn, imgStoreId);
char *who = g_strdup_printf ("%d", tgl_get_peer_id (U->id));
if (dld->get_user_info_data->show_info == 1) {
PurpleNotifyUserInfo *info = create_user_notify_info(U);
p2tgl_buddy_icons_set_for_user (conn->pa, &P->id, filename);
if (dld->get_user_info_data->peer && dld->get_user_info_data->peer->encr_chat.first_key_sha[0]) {
// display secret key
int sha1key_store_id = generate_ident_icon (conn->TLS, dld->get_user_info_data->peer->encr_chat.first_key_sha);
if (sha1key_store_id != -1) {
char *ident_icon = format_img_full (sha1key_store_id);
purple_notify_user_info_add_pair (info, "Secret key", ident_icon);
g_free(ident_icon);
}
g_free (who);
who = g_strdup_printf ("%d", tgl_get_peer_id (dld->get_user_info_data->peer->id));
if (dld->get_user_info_data->show_info == 1) {
PurpleNotifyUserInfo *info = p2tgl_notify_peer_info_new (TLS, P);
p2tgl_notify_userinfo (TLS, P->id, info, NULL, NULL);
}
char *profile_image = profile_image = format_img_full (imgStoreId);
purple_notify_user_info_add_pair (info, "Profile image", profile_image);
purple_notify_userinfo (conn->gc, who, info, NULL, NULL);
g_free (profile_image);
}
if (dld->get_user_info_data->peer) {
char *id = g_strdup_printf ("%d", tgl_get_peer_id (dld->get_user_info_data->peer->id));
gchar *img_data = NULL;
size_t len;
GError *err = NULL;
g_file_get_contents (filename, &img_data, &len, &err);
purple_buddy_icons_set_for_user(conn->pa, id, img_data, len, NULL);
g_free (id);
}
purple_buddy_icons_set_for_user(conn->pa, who, data, len, NULL);
g_free(who);
g_free(dld->get_user_info_data);
fin:
free (dld->get_user_info_data);
free (dld);
}
void on_user_get_info (struct tgl_state *TLS, void *info_data, int success, struct tgl_user *U)
{
get_user_info_data *user_info_data = (get_user_info_data *)info_data;
tgl_peer_t *P = tgl_peer_get (TLS, user_info_data->peer);
if (! success) {
warning ("on_user_get_info not successfull, aborting...\n");
return;
}
get_user_info_data *user_info_data = (get_user_info_data *)info_data;
if (U->photo.sizes_num == 0) {
// No profile pic to load, display it right away
if (user_info_data->show_info) {
PurpleNotifyUserInfo *info = create_user_notify_info(U);
if (user_info_data->peer && user_info_data->peer->encr_chat.first_key_sha[0]) {
// display secret key
int sha1key_store_id = generate_ident_icon (TLS, user_info_data->peer->encr_chat.first_key_sha);
if (sha1key_store_id != -1) {
char *ident_icon = format_img_full (sha1key_store_id);
purple_notify_user_info_add_pair (info, "Secret key", ident_icon);
g_free (ident_icon);
}
p2tgl_notify_userinfo (TLS, user_info_data->peer->id, info, NULL, NULL);
} else {
p2tgl_notify_userinfo (TLS, U->id, info, NULL, NULL);
}
PurpleNotifyUserInfo *info = p2tgl_notify_peer_info_new (TLS, P);
p2tgl_notify_userinfo (TLS, P->id, info, NULL, NULL);
}
g_free(user_info_data);
g_free (user_info_data);
} else {
struct download_desc *dld = malloc (sizeof(struct download_desc));
dld->data = U;
@ -794,7 +723,10 @@ static void tgprpl_tooltip_text (PurpleBuddy * buddy, PurpleNotifyUserInfo * inf
return;
}
purple_notify_user_info_add_pair (info, "Status", format_status(&P->user.status));
purple_notify_user_info_add_pair (info, "Last seen: ", format_time(P->user.status.when));
gchar *status = format_user_status (&P->user.status);
purple_notify_user_info_add_pair (info, "last online: ", status);
g_free (status);
}
static GList *tgprpl_status_types (PurpleAccount * acct) {
@ -954,36 +886,21 @@ static void tgprpl_get_info (PurpleConnection * gc, const char *who) {
tgl_peer_t *peer = find_peer_by_name (conn->TLS, who);
if (! peer) { return; }
get_user_info_data* info_data = malloc (sizeof(get_user_info_data));
info_data->show_info = 1;
info_data->peer = peer;
get_user_info_data* info_data = get_user_info_data_new (1, peer->id);
switch (tgl_get_peer_type (peer->id)) {
case TGL_PEER_USER:
case TGL_PEER_CHAT:
tgl_do_get_user_info (conn->TLS, peer->id, 0, on_user_get_info, info_data);
break;
case TGL_PEER_ENCR_CHAT: {
tgl_peer_t *parent_peer;
if (peer->encr_chat.state == sc_waiting) {
PurpleNotifyUserInfo *info = purple_notify_user_info_new();
purple_notify_user_info_add_pair (info, "", "Waiting for user to get online ...");
p2tgl_notify_userinfo (conn->TLS, peer->id, info, NULL, NULL);
return;
}
if (peer->encr_chat.admin_id == conn->TLS->our_id){
parent_peer = tgl_peer_get (conn->TLS, TGL_MK_USER(peer->encr_chat.user_id));
} else {
parent_peer = tgl_peer_get (conn->TLS, TGL_MK_USER(peer->encr_chat.admin_id));
}
if (parent_peer){
tgl_do_get_user_info(conn->TLS, parent_peer->id, 0, on_user_get_info, info_data);
tgl_peer_t *parent_peer = tgp_encr_chat_get_partner(conn->TLS, &peer->encr_chat);
if (parent_peer) {
tgl_do_get_user_info (conn->TLS, parent_peer->id, 0, on_user_get_info, info_data);
}
break;
}
}
}

View file

@ -42,5 +42,6 @@ void on_ready (struct tgl_state *TLS);
extern const char *pk_path;
extern const char *config_dir;
extern PurplePlugin *_telegram_protocol;
char *format_user_status (struct tgl_user_status *status);
#endif

View file

@ -20,6 +20,9 @@
#include "telegram-purple.h"
#include "tgp-2prpl.h"
#include "tgp-structs.h"
#include "telegram-purple.h"
#include "tgp-utils.h"
#include "telegram-base.h"
#include <server.h>
#include <tgl.h>
@ -174,36 +177,10 @@ void p2tgl_prpl_got_set_status_offline (struct tgl_state *TLS, tgl_peer_id_t use
}
void p2tgl_prpl_got_user_status (struct tgl_state *TLS, tgl_peer_id_t user, struct tgl_user_status *status) {
if (status->online == 1) {
char *name = p2tgl_peer_strdup_id (user);
purple_prpl_got_user_status (tg_get_acc(TLS), name, "available", NULL);
g_free (name);
p2tgl_prpl_got_set_status_offline(TLS, user);
} else {
char *name = p2tgl_peer_strdup_id (user);
char *when;
switch (status->online) {
case -1:
when = g_strdup_printf("%d", status->when);
break;
case -2:
when = g_strdup_printf("recently");
break;
case -3:
when = g_strdup_printf("last week");
break;
case -4:
when = g_strdup_printf("last month");
break;
default:
when = g_strdup ("unknown");
break;
}
purple_prpl_got_user_status (tg_get_acc(TLS), name, "mobile", "last online", when, NULL);
g_free(name);
g_free(when);
p2tgl_prpl_got_set_status_mobile(TLS, user);
}
}
@ -269,3 +246,105 @@ void p2tgl_blist_alias_buddy (PurpleBuddy *buddy, struct tgl_user *user) {
g_free(name);
}
PurpleNotifyUserInfo *p2tgl_notify_user_info_new (struct tgl_user *U) {
PurpleNotifyUserInfo *info = purple_notify_user_info_new();
if (str_not_empty(U->first_name) && str_not_empty(U->last_name)) {
purple_notify_user_info_add_pair (info, "First name", U->first_name);
purple_notify_user_info_add_pair (info, "Last name", U->last_name);
} else {
purple_notify_user_info_add_pair (info, "Name", U->print_name);
}
if (str_not_empty (U->username)) {
purple_notify_user_info_add_pair (info, "Username", U->username);
}
char *status = format_user_status (&U->status);
purple_notify_user_info_add_pair (info, "Last seen", status);
g_free (status);
if (str_not_empty (U->phone)) {
char *phone = g_strdup_printf("+%s", U->phone);
purple_notify_user_info_add_pair (info, "Phone", phone);
g_free (phone);
}
return info;
}
PurpleNotifyUserInfo *p2tgl_notify_encrypted_chat_info_new (struct tgl_state *TLS,
struct tgl_secret_chat *secret, struct tgl_user *U) {
PurpleNotifyUserInfo *info = p2tgl_notify_user_info_new (U);
if (secret->state == sc_waiting) {
purple_notify_user_info_add_pair (info, "", "Waiting for user to get online ...");
return info;
}
const char *ttl_key = "Self destructiom timer";
if (secret->ttl) {
char *ttl = g_strdup_printf ("%d", secret->ttl);
purple_notify_user_info_add_pair (info, ttl_key, ttl);
g_free (ttl);
} else {
purple_notify_user_info_add_pair (info, ttl_key, "Off");
}
if (secret->first_key_sha[0]) {
int sha1key_store_id = generate_ident_icon (TLS, secret->first_key_sha);
if (sha1key_store_id != -1) {
char *ident_icon = format_img_full (sha1key_store_id);
purple_notify_user_info_add_pair (info, "Secret key", ident_icon);
g_free(ident_icon);
}
}
return info;
}
PurpleNotifyUserInfo *p2tgl_notify_peer_info_new (struct tgl_state *TLS, tgl_peer_t *P) {
switch (tgl_get_peer_type (P->id)) {
case TGL_PEER_ENCR_CHAT: {
struct tgl_secret_chat *chat = &P->encr_chat;
tgl_peer_t *partner = tgp_encr_chat_get_partner (TLS, chat);
return p2tgl_notify_encrypted_chat_info_new (TLS, chat, &partner->user);
break;
}
case TGL_PEER_USER:
return p2tgl_notify_user_info_new (&P->user);
break;
default:
return purple_notify_user_info_new ();
}
}
int p2tgl_imgstore_add_with_id (const char* filename)
{
gchar *data = NULL;
size_t len;
GError *err = NULL;
g_file_get_contents (filename, &data, &len, &err);
int id = purple_imgstore_add_with_id (data, len, NULL);
return id;
}
void p2tgl_buddy_icons_set_for_user (PurpleAccount *pa, tgl_peer_id_t *id, const char* filename)
{
char *who = g_strdup_printf("%d", tgl_get_peer_id(*id));
gchar *data = NULL;
size_t len;
GError *err = NULL;
g_file_get_contents (filename, &data, &len, &err);
purple_buddy_icons_set_for_user (pa, who, data, len, NULL);
g_free (who);
}

View file

@ -68,5 +68,11 @@ PurpleChat *p2tgl_chat_find (struct tgl_state *TLS, tgl_peer_id_t chat);
void *p2tgl_notify_userinfo(struct tgl_state *TLS, tgl_peer_id_t user, PurpleNotifyUserInfo *user_info, PurpleNotifyCloseCallback cb, gpointer user_data);
PurpleNotifyUserInfo *p2tgl_notify_peer_info_new (struct tgl_state *TLS, tgl_peer_t *P);
PurpleNotifyUserInfo *p2tgl_notify_user_info_new (struct tgl_user *U);
PurpleNotifyUserInfo *p2tgl_notify_encrypted_chat_info_new (struct tgl_state *TLS, struct tgl_secret_chat *secret, struct tgl_user *U);
void p2tgl_blist_alias_buddy (PurpleBuddy *buddy, struct tgl_user *user);
int p2tgl_imgstore_add_with_id (const char* filename);
void p2tgl_buddy_icons_set_for_user (PurpleAccount *pa, tgl_peer_id_t *id, const char* filename);
#endif

View file

@ -21,6 +21,7 @@
#include "tgp-structs.h"
#include "purple.h"
#include "msglog.h"
#include "tgp-utils.h"
#include <glib.h>
#include <tgl.h>
@ -90,16 +91,6 @@ static void used_image_free (gpointer data)
debug ("used_image: unref %d", id);
}
static void queue_free_full (GQueue *queue, GDestroyNotify free_func)
{
void *entry;
while ((entry = g_queue_pop_head(queue))) {
free_func (entry);
}
g_queue_free (queue);
}
void used_images_add (connection_data *data, gint imgid)
{
data->used_images = g_list_append (data->used_images, GINT_TO_POINTER(imgid));
@ -121,12 +112,20 @@ connection_data *connection_data_init (struct tgl_state *TLS, PurpleConnection *
void *connection_data_free (connection_data *conn)
{
purple_timeout_remove(conn->timer);
queue_free_full (conn->pending_reads, pending_reads_free_cb);
queue_free_full (conn->new_messages, message_text_free);
tgp_g_queue_free_full (conn->pending_reads, pending_reads_free_cb);
tgp_g_queue_free_full (conn->new_messages, message_text_free);
g_hash_table_destroy (conn->joining_chats);
g_list_free_full (conn->used_images, used_image_free);
tgl_free_all (conn->TLS);
free (conn->TLS);
free (conn);
return NULL;
}
get_user_info_data* get_user_info_data_new (int show_info, tgl_peer_id_t peer)
{
get_user_info_data *info_data = malloc (sizeof(get_user_info_data));
info_data->show_info = show_info;
info_data->peer = peer;
return info_data;
}

View file

@ -42,8 +42,8 @@ typedef struct {
typedef struct {
int show_info;
tgl_peer_t *peer;
}get_user_info_data;
tgl_peer_id_t peer;
} get_user_info_data;
struct download_desc {
get_user_info_data *get_user_info_data;
@ -66,4 +66,6 @@ void used_images_add (connection_data *data, gint imgid);
void *connection_data_free (connection_data *conn);
connection_data *connection_data_init (struct tgl_state *TLS, PurpleConnection *gc, PurpleAccount *pa);
get_user_info_data* get_user_info_data_new (int show_info, tgl_peer_id_t peer);
#endif

87
tgp-utils.c Normal file
View file

@ -0,0 +1,87 @@
/*
This file is part of telegram-purple
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
Copyright Matthias Jentsch 2014
*/
#include "tgp-utils.h"
#include "purple.h"
const char *format_time (time_t date) {
struct tm *datetime = localtime(&date);
return purple_utf8_strftime ("%d.%m.%Y %H:%M", datetime);
}
char *format_img_full (int imgstore) {
const char *br = "<br>";
// <br>'s look ugly in Adium, but no <br> will look ugly in Pidgin
#ifdef __ADIUM_
br = "";
#endif
return g_strdup_printf ("%s<img id=\"%u\">", br, imgstore);
}
int str_not_empty (const char *string) {
return string && string[0] != '\0';
}
/**
* Return whether this message was created by our client in this session
*/
int our_msg (struct tgl_state *TLS, struct tgl_message *M) {
return (M->flags & FLAG_SESSION_OUTBOUND) != 0;
}
/**
* Return whether this message was created by the current user.
*/
int out_msg (struct tgl_state *TLS, struct tgl_message *M) {
return M->out;
}
tgl_peer_t *tgp_encr_chat_get_partner (struct tgl_state *TLS, struct tgl_secret_chat *chat)
{
return tgl_peer_get (TLS, TGL_MK_USER(chat->admin_id == TLS->our_id ? chat->user_id : chat->admin_id));
}
char *tgp_g_format_size (gint64 size) {
char *sizes[] = {
"B",
"KB",
"MB",
"GB",
"PB"
};
int base = 0;
double s = (double) size;
while (s > 1024 && base < 4) {
s /= 1024;
++ base;
}
return g_strdup_printf ("%.1f %s, ", s, sizes[base]);
}
void tgp_g_queue_free_full (GQueue *queue, GDestroyNotify free_func)
{
void *entry;
while ((entry = g_queue_pop_head(queue))) {
free_func (entry);
}
g_queue_free (queue);
}

39
tgp-utils.h Normal file
View file

@ -0,0 +1,39 @@
/*
This file is part of telegram-purple
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
Copyright Matthias Jentsch 2014
*/
#ifndef __telegram_adium__tgp_util__
#define __telegram_adium__tgp_util__
#include <stdio.h>
#include <time.h>
#include <tgl.h>
#include <glib.h>
tgl_peer_t *tgp_encr_chat_get_partner (struct tgl_state *TLS, struct tgl_secret_chat *chat);
const char *format_time (time_t date);
char *format_img_full (int imgstore);
int str_not_empty (const char *string);
int our_msg (struct tgl_state *TLS, struct tgl_message *M);
int out_msg (struct tgl_state *TLS, struct tgl_message *M);
char *tgp_g_format_size (gint64 size);
void tgp_g_queue_free_full (GQueue *queue, GDestroyNotify free_func);
#endif /* defined(__telegram_adium__tgp_util__) */