1 /* liblivejournal - a client library for LiveJournal.
2 * Copyright (C) 2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
11 #include <stdlib.h> /* atoi. */
12 #include <string.h> /* strchr. */
14 #include "liblj/entry.h"
15 #include "liblj/friends.h"
16 #include "liblj/types.h"
19 _lj_nid_by_id(GSList
*l
, int id
) {
20 for ( ; l
; l
= l
->next
) {
21 if (((_LJNameIDHash
*)l
->data
)->id
== id
)
22 return ((_LJNameIDHash
*)l
->data
)->name
;
27 _lj_nid_by_name(GSList
*l
, const char* name
) {
28 for ( ; l
; l
= l
->next
) {
29 if (strcmp(((_LJNameIDHash
*)l
->data
)->name
, name
) == 0)
30 return ((_LJNameIDHash
*)l
->data
)->id
;
35 _lj_nid_compare_alpha(_LJNameIDHash
*a
, _LJNameIDHash
*b
) {
36 return g_ascii_strcasecmp(a
->name
, b
->name
);
40 _lj_nid_free(_LJNameIDHash
*nid
) {
47 LJFriend
*f
= g_new0(LJFriend
, 1);
48 f
->foreground
= 0x000000;
49 f
->background
= 0xFFFFFF;
54 lj_friend_type_from_str(char *str
) {
56 if (strcmp(str
, "community") == 0)
57 return LJ_FRIEND_TYPE_COMMUNITY
;
59 return LJ_FRIEND_TYPE_USER
;
63 lj_friend_free(LJFriend
*f
) {
70 lj_friend_compare_username(gconstpointer a
, gconstpointer b
) {
71 const LJFriend
*fa
= a
;
72 const LJFriend
*fb
= b
;
74 return strcmp(fa
->username
, fb
->username
);
78 lj_security_append_to_request(LJSecurity
*security
, LJRequest
*request
) {
81 switch (security
->type
) {
82 case LJ_SECURITY_PUBLIC
:
83 text
= "public"; break;
84 case LJ_SECURITY_PRIVATE
:
85 text
= "private"; break;
86 case LJ_SECURITY_FRIENDS
:
87 case LJ_SECURITY_CUSTOM
:
88 text
= "usemask"; break;
90 lj_request_add(request
, "security", text
);
92 if (security
->type
== LJ_SECURITY_FRIENDS
) {
93 lj_request_add_int(request
, "allowmask", LJ_SECURITY_ALLOWMASK_FRIENDS
);
94 } else if (security
->type
== LJ_SECURITY_CUSTOM
) {
95 lj_request_add_int(request
, "allowmask", security
->allowmask
);
100 lj_security_from_strings(LJSecurity
*security
, const char *sectext
, const char *allowmask
) {
102 security
->type
= LJ_SECURITY_PUBLIC
;
106 if (g_ascii_strcasecmp(sectext
, "public") == 0) {
107 security
->type
= LJ_SECURITY_PUBLIC
;
108 } else if (g_ascii_strcasecmp(sectext
, "private") == 0) {
109 security
->type
= LJ_SECURITY_PRIVATE
;
110 } else if (g_ascii_strcasecmp(sectext
, "friends") == 0) {
111 security
->type
= LJ_SECURITY_FRIENDS
;
112 } else if (g_ascii_strcasecmp(sectext
, "usemask") == 0 ||
113 g_ascii_strcasecmp(sectext
, "custom") == 0) {
116 am
= atoi(allowmask
);
120 security
->type
= LJ_SECURITY_PRIVATE
; break;
121 case LJ_SECURITY_ALLOWMASK_FRIENDS
:
122 security
->type
= LJ_SECURITY_FRIENDS
; break;
124 security
->type
= LJ_SECURITY_CUSTOM
;
125 security
->allowmask
= am
;
128 g_warning("security: '%s' unhandled", sectext
);
134 security_load_from_result(LJSecurity
*security
, NetResult
*result
) {
135 char *sectext
, *allowmask
;
137 sectext
= net_result_get(result
, "security");
139 allowmask
= net_result_get(result
, "allowmask");
140 security_load_from_strings(security
, sectext
, allowmask
);
146 lj_security_to_strings(LJSecurity
*security
, char **sectext
, char **allowmask
) {
148 switch (security
->type
) {
149 case LJ_SECURITY_PUBLIC
: return;
150 case LJ_SECURITY_PRIVATE
: type
= "private"; break;
151 case LJ_SECURITY_FRIENDS
: type
= "friends"; break;
152 case LJ_SECURITY_CUSTOM
: type
= "custom"; break;
155 *sectext
= g_strdup(type
);
157 if (allowmask
&& security
->type
== LJ_SECURITY_CUSTOM
)
158 *allowmask
= g_strdup_printf("%ud", security
->allowmask
);
162 lj_timegm(struct tm
*tm
) {
165 #elif defined(WIN32_FAKE_TIMEGM)
166 /* this is only for non-cygwin builds.
167 * windows getenv/putenv works in a wacky way. */
176 char *putstr
= g_strdup_printf("TZ=%s", tz
);
185 /* on systems that lack timegm, we can fake it with environment trickery.
186 * this is taken from the timegm(3) manpage. */
204 lj_ljdate_to_tm(const char *ljtime
, struct tm
*ptm
) {
206 if (sscanf(ljtime
, "%4d-%2d-%2d %2d:%2d:%2d",
207 &ptm
->tm_year
, &ptm
->tm_mon
, &ptm
->tm_mday
,
208 &ptm
->tm_hour
, &ptm
->tm_min
, &ptm
->tm_sec
) < 5)
210 ptm
->tm_year
-= 1900;
212 ptm
->tm_isdst
= -1; /* -1: "the information is not available" */
217 lj_tm_to_ljdate(struct tm
*ptm
) {
218 return g_strdup_printf("%04d-%02d-%02d %02d:%02d:%02d",
219 ptm
->tm_year
+1900, ptm
->tm_mon
+1, ptm
->tm_mday
,
220 ptm
->tm_hour
, ptm
->tm_min
, ptm
->tm_sec
);
224 lj_tm_to_ljdate_noseconds(struct tm
*ptm
) {
225 return g_strdup_printf("%04d-%02d-%02d %02d:%02d",
226 ptm
->tm_year
+1900, ptm
->tm_mon
+1, ptm
->tm_mday
,
227 ptm
->tm_hour
, ptm
->tm_min
);
231 lj_color_to_int(const char *color
) {
234 return strtol(color
+1, NULL
, 16);
238 lj_int_to_color(guint32 l
, char *color
) {
239 sprintf(color
, "#%06X", l
);