2015-01-21 23:49:42 +01:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
2015-03-12 01:47:57 +01:00
|
|
|
Copyright Matthias Jentsch 2014-2015
|
2015-01-21 23:49:42 +01:00
|
|
|
*/
|
|
|
|
|
2015-10-04 17:12:04 +02:00
|
|
|
#include "telegram-purple.h"
|
2015-05-24 00:22:31 +02:00
|
|
|
|
2015-01-21 23:49:42 +01:00
|
|
|
const char *format_time (time_t date) {
|
2015-11-28 20:17:47 +01:00
|
|
|
// TODO: Inline this function for better readability?
|
2015-01-21 23:49:42 +01:00
|
|
|
struct tm *datetime = localtime(&date);
|
2015-11-28 20:17:47 +01:00
|
|
|
// This should be the language's timestamp format. This is preceded by a colon.
|
2015-10-07 21:23:03 +02:00
|
|
|
return purple_utf8_strftime (_("%d.%m.%Y %H:%M"), datetime);
|
2015-01-21 23:49:42 +01:00
|
|
|
}
|
|
|
|
|
2015-07-30 11:45:50 +02:00
|
|
|
char *tgp_format_img (int imgstore) {
|
2015-01-21 23:49:42 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-13 16:57:08 +01:00
|
|
|
char *tgp_format_user_status (struct tgl_user_status *status) {
|
|
|
|
char *when;
|
|
|
|
switch (status->online) {
|
|
|
|
case -1:
|
2015-10-19 20:10:47 +02:00
|
|
|
when = g_strdup_printf ("%s", format_time (status->when));
|
2015-03-13 16:57:08 +01:00
|
|
|
break;
|
|
|
|
case -2:
|
2015-11-28 20:17:47 +01:00
|
|
|
// This is preceded by a colon.
|
2015-10-19 20:10:47 +02:00
|
|
|
when = g_strdup (_("recently"));
|
2015-03-13 16:57:08 +01:00
|
|
|
break;
|
|
|
|
case -3:
|
2015-11-28 20:17:47 +01:00
|
|
|
// This is preceded by a colon.
|
2015-10-19 20:10:47 +02:00
|
|
|
when = g_strdup (_("last week"));
|
2015-03-13 16:57:08 +01:00
|
|
|
break;
|
|
|
|
case -4:
|
2015-11-28 20:17:47 +01:00
|
|
|
// This is preceded by a colon.
|
2015-10-19 20:10:47 +02:00
|
|
|
when = g_strdup (_("last month"));
|
2015-03-13 16:57:08 +01:00
|
|
|
break;
|
|
|
|
default:
|
2015-11-28 20:17:47 +01:00
|
|
|
// This is preceded by a colon. It refers to a point on time.
|
2015-10-07 21:23:03 +02:00
|
|
|
when = g_strdup (_("unknown"));
|
2015-03-13 16:57:08 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return when;
|
|
|
|
}
|
|
|
|
|
2015-01-21 23:49:42 +01:00
|
|
|
int str_not_empty (const char *string) {
|
|
|
|
return string && string[0] != '\0';
|
|
|
|
}
|
|
|
|
|
2015-05-23 00:18:26 +02:00
|
|
|
int tgp_outgoing_msg (struct tgl_state *TLS, struct tgl_message *M) {
|
2015-05-25 22:36:30 +02:00
|
|
|
return (M->flags & TGLMF_SESSION_OUTBOUND);
|
2015-01-21 23:49:42 +01:00
|
|
|
}
|
|
|
|
|
2015-05-23 00:18:26 +02:00
|
|
|
int tgp_our_msg (struct tgl_state *TLS, struct tgl_message *M) {
|
2015-10-02 17:02:03 +02:00
|
|
|
return tgl_get_peer_id (TLS->our_id) == tgl_get_peer_id (M->from_id);
|
2015-01-21 23:49:42 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 20:56:50 +01:00
|
|
|
tgl_peer_t *tgp_encr_chat_get_partner (struct tgl_state *TLS, struct tgl_secret_chat *chat) {
|
2015-10-02 17:02:03 +02:00
|
|
|
return tgl_peer_get (TLS, TGL_MK_USER(chat->admin_id == tgl_get_peer_id (TLS->our_id) ? chat->user_id : chat->admin_id));
|
2015-01-21 23:49:42 +01:00
|
|
|
}
|
|
|
|
|
2015-03-13 16:57:08 +01:00
|
|
|
long tgp_time_n_days_ago (int days) {
|
2015-03-28 12:22:19 +01:00
|
|
|
return time(NULL) - 24 * 3600 * days;
|
2015-03-13 16:57:08 +01:00
|
|
|
};
|
|
|
|
|
2015-01-23 20:56:50 +01:00
|
|
|
void tgp_g_queue_free_full (GQueue *queue, GDestroyNotify free_func) {
|
2015-01-21 23:49:42 +01:00
|
|
|
void *entry;
|
|
|
|
|
|
|
|
while ((entry = g_queue_pop_head(queue))) {
|
|
|
|
free_func (entry);
|
|
|
|
}
|
|
|
|
g_queue_free (queue);
|
|
|
|
}
|
2015-02-23 20:02:24 +01:00
|
|
|
|
|
|
|
void tgp_g_list_free_full (GList *list, GDestroyNotify free_func) {
|
2015-12-18 22:59:51 +01:00
|
|
|
if (list) {
|
|
|
|
g_list_foreach (list, (GFunc)free_func, NULL);
|
|
|
|
g_list_free (list);
|
|
|
|
}
|
2015-02-23 20:02:24 +01:00
|
|
|
}
|
2015-05-23 03:07:44 +02:00
|
|
|
|
|
|
|
const char *tgp_mime_to_filetype (const char *mime) {
|
|
|
|
int len = (int) strlen (mime);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len - 1; i ++) {
|
|
|
|
if (mime[i] == '/') {
|
|
|
|
return mime + i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-07-11 19:32:59 +02:00
|
|
|
|
|
|
|
int tgp_startswith (const char *str, const char *with) {
|
|
|
|
if (! str || !with) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
int slen = strlen (str), wlen = strlen (with);
|
|
|
|
if (wlen > slen) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
while (*with) if (*str++ != *with++) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-11-23 01:33:20 +01:00
|
|
|
|
|
|
|
void tgp_replace (char *string, char what, char with) {
|
|
|
|
char *p = string;
|
|
|
|
while (*(p ++)) {
|
|
|
|
if (*p == what) {
|
|
|
|
*p = with;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|