we don't really need to build src/liblj/ as library; and we don't need to write each...
[k8lowj.git] / src / liblj / types.c
blobf51cae56def2d2a4fdba45feabdc7b65f7b87d26
1 /* liblivejournal - a client library for LiveJournal.
2 * Copyright (C) 2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
7 #include <glib.h>
9 #include <stdio.h>
10 #include <errno.h>
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"
18 char*
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;
24 return NULL;
26 int
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;
32 return -1;
34 gint
35 _lj_nid_compare_alpha(_LJNameIDHash *a, _LJNameIDHash *b) {
36 return g_ascii_strcasecmp(a->name, b->name);
39 void
40 _lj_nid_free(_LJNameIDHash *nid) {
41 g_free(nid->name);
42 g_free(nid);
45 LJFriend*
46 lj_friend_new(void) {
47 LJFriend *f = g_new0(LJFriend, 1);
48 f->foreground = 0x000000;
49 f->background = 0xFFFFFF;
50 return f;
53 LJFriendType
54 lj_friend_type_from_str(char *str) {
55 if (str) {
56 if (strcmp(str, "community") == 0)
57 return LJ_FRIEND_TYPE_COMMUNITY;
59 return LJ_FRIEND_TYPE_USER;
62 void
63 lj_friend_free(LJFriend *f) {
64 g_free(f->username);
65 g_free(f->fullname);
66 g_free(f);
69 gint
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);
77 void
78 lj_security_append_to_request(LJSecurity *security, LJRequest *request) {
79 char *text = NULL;
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);
99 void
100 lj_security_from_strings(LJSecurity *security, const char *sectext, const char *allowmask) {
101 if (!sectext) {
102 security->type = LJ_SECURITY_PUBLIC;
103 return;
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) {
114 unsigned int am = 0;
115 if (allowmask)
116 am = atoi(allowmask);
118 switch (am) {
119 case 0:
120 security->type = LJ_SECURITY_PRIVATE; break;
121 case LJ_SECURITY_ALLOWMASK_FRIENDS:
122 security->type = LJ_SECURITY_FRIENDS; break;
123 default:
124 security->type = LJ_SECURITY_CUSTOM;
125 security->allowmask = am;
127 } else {
128 g_warning("security: '%s' unhandled", sectext);
132 #if 0
133 void
134 security_load_from_result(LJSecurity *security, NetResult *result) {
135 char *sectext, *allowmask;
137 sectext = net_result_get(result, "security");
138 if (sectext) {
139 allowmask = net_result_get(result, "allowmask");
140 security_load_from_strings(security, sectext, allowmask);
143 #endif
145 void
146 lj_security_to_strings(LJSecurity *security, char **sectext, char **allowmask) {
147 char *type = NULL;
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;
154 if (sectext)
155 *sectext = g_strdup(type);
157 if (allowmask && security->type == LJ_SECURITY_CUSTOM)
158 *allowmask = g_strdup_printf("%ud", security->allowmask);
161 time_t
162 lj_timegm(struct tm *tm) {
163 #ifdef HAVE_TIMEGM
164 return timegm(tm);
165 #elif defined(WIN32_FAKE_TIMEGM)
166 /* this is only for non-cygwin builds.
167 * windows getenv/putenv works in a wacky way. */
168 time_t ret;
169 char *tz;
171 tz = getenv("TZ");
172 putenv("TZ=UTC");
173 tzset();
174 ret = mktime(tm);
175 if (tz) {
176 char *putstr = g_strdup_printf("TZ=%s", tz);
177 putenv(putstr);
178 g_free(putstr);
179 } else {
180 putenv("TZ=");
182 tzset();
183 return ret;
184 #else
185 /* on systems that lack timegm, we can fake it with environment trickery.
186 * this is taken from the timegm(3) manpage. */
187 time_t ret;
188 char *tz;
190 tz = getenv("TZ");
191 setenv("TZ", "", 1);
192 tzset();
193 ret = mktime(tm);
194 if (tz)
195 setenv("TZ", tz, 1);
196 else
197 unsetenv("TZ");
198 tzset();
199 return ret;
200 #endif
203 gboolean
204 lj_ljdate_to_tm(const char *ljtime, struct tm *ptm) {
205 gboolean ret = TRUE;
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)
209 ret = FALSE;
210 ptm->tm_year -= 1900;
211 ptm->tm_mon -= 1;
212 ptm->tm_isdst = -1; /* -1: "the information is not available" */
213 return ret;
216 char*
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);
223 char*
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);
230 guint32
231 lj_color_to_int(const char *color) {
232 if (color[0] != '#')
233 return 0;
234 return strtol(color+1, NULL, 16);
237 void
238 lj_int_to_color(guint32 l, char *color) {
239 sprintf(color, "#%06X", l);