summaryrefslogtreecommitdiff
path: root/libpurple/buddyicon.h
blob: 3e4c7007b8e83fdfb4309df0645916a8c650bfa3 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * 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
 */

#if !defined(PURPLE_GLOBAL_HEADER_INSIDE) && !defined(PURPLE_COMPILATION)
# error "only <purple.h> may be included directly"
#endif

#ifndef PURPLE_BUDDYICON_H
#define PURPLE_BUDDYICON_H

#include <glib.h>
#include <gio/gio.h>

#define PURPLE_TYPE_BUDDY_ICON (purple_buddy_icon_get_type())

/**
 * PurpleBuddyIcon:
 *
 * An opaque structure representing a buddy icon for a particular user on a
 * particular #PurpleAccount.  Instances are reference-counted; use
 * purple_buddy_icon_ref() and purple_buddy_icon_unref() to take and release
 * references.
 */
typedef struct _PurpleBuddyIcon PurpleBuddyIcon;

#define PURPLE_TYPE_BUDDY_ICON_SPEC  (purple_buddy_icon_spec_get_type())

typedef struct _PurpleBuddyIconSpec PurpleBuddyIconSpec;

#include "account.h"
#include "blistnode.h"
#include "buddylist.h"
#include "image.h"
#include "protocols.h"
#include "util.h"

/**
 * PurpleBuddyIconScaleFlags:
 * @PURPLE_ICON_SCALE_DISPLAY: We scale the icon when we display it
 * @PURPLE_ICON_SCALE_SEND:    We scale the icon before we send it to the server
 */
typedef enum  /*< flags >*/
{
	PURPLE_ICON_SCALE_DISPLAY = 0x01,
	PURPLE_ICON_SCALE_SEND    = 0x02

} PurpleBuddyIconScaleFlags;

/**
 * PurpleBuddyIconSpec:
 * @format: This is a comma-delimited list of image formats or %NULL if icons
 *          are not supported.  Neither the core nor the protocol will actually
 *          check to see if the data it's given matches this; it's entirely up
 *          to the UI to do what it wants
 * @min_width:    Minimum width of this icon
 * @min_height:   Minimum height of this icon
 * @max_width:    Maximum width of this icon
 * @max_height:   Maximum height of this icon
 * @max_filesize: Maximum size in bytes
 * @scale_rules:  How to stretch this icon
 *
 * A description of a Buddy Icon specification.  This tells Purple what kind of
 * image file it should give a protocol, and what kind of image file it should
 * expect back. Dimensions less than 1 should be ignored and the image not
 * scaled.
 */
struct _PurpleBuddyIconSpec {
	char *format;
	int min_width;
	int min_height;
	int max_width;
	int max_height;
	size_t max_filesize;
	PurpleBuddyIconScaleFlags scale_rules;
};

G_BEGIN_DECLS

/**************************************************************************/
/* Buddy Icon API                                                         */
/**************************************************************************/

/**
 * purple_buddy_icon_get_type:
 *
 * Returns: The #GType for the #PurpleBuddyIcon boxed structure.
 */
GType purple_buddy_icon_get_type(void);

/**
 * purple_buddy_icon_new:
 * @account:   The account the user is on.
 * @username:  The username the icon belongs to.
 * @icon_data: The buddy icon data.
 * @icon_len:  The buddy icon length.
 * @checksum:  A protocol checksum from the protocol or %NULL.
 *
 * Creates a new buddy icon structure and populates it.
 *
 * If an icon for this account+username already exists, you'll get a reference
 * to that structure, which will have been updated with the data supplied.
 *
 * Returns: The buddy icon structure, with a reference for the caller.
 */
PurpleBuddyIcon *purple_buddy_icon_new(PurpleAccount *account, const char *username,
                                       void *icon_data, size_t icon_len,
                                       const char *checksum);

/**
 * purple_buddy_icon_ref:
 * @icon: The buddy icon.
 *
 * Increments the reference count on a buddy icon.
 *
 * Returns: @icon.
 */
PurpleBuddyIcon *purple_buddy_icon_ref(PurpleBuddyIcon *icon);

/**
 * purple_buddy_icon_unref:
 * @icon: The buddy icon.
 *
 * Decrements the reference count on a buddy icon.
 *
 * If the reference count reaches 0, the icon will be destroyed.
 */
void purple_buddy_icon_unref(PurpleBuddyIcon *icon);

/**
 * purple_buddy_icon_update:
 * @icon: The buddy icon.
 *
 * Updates every instance of this icon.
 */
void purple_buddy_icon_update(PurpleBuddyIcon *icon);

/**
 * purple_buddy_icon_set_data:
 * @icon: The buddy icon.
 * @data: (transfer full): The buddy icon data.
 * @len: The length of the data in @data.
 * @checksum: A protocol checksum from the protocol or %NULL.
 *
 * Sets the buddy icon's data.
 */
void
purple_buddy_icon_set_data(PurpleBuddyIcon *icon, guchar *data,
                           size_t len, const char *checksum);

/**
 * purple_buddy_icon_save_to_filename:
 * @icon: The #PurpleBuddyIcon instance.
 * @filename: The filename to write.
 * @error: Return address for a #GError, or %NULL.
 *
 * Writes the contents of @icon to @filename.
 *
 * Returns: %TRUE on success, or %FALSE on error possibly with @error set.
 *
 * Since: 3.0.0
 */
gboolean purple_buddy_icon_save_to_filename(PurpleBuddyIcon *icon, const gchar *filename, GError **error);

/**
 * purple_buddy_icon_get_account:
 * @icon: The buddy icon.
 *
 * Returns the buddy icon's account.
 *
 * Returns: (transfer none): The account.
 */
PurpleAccount *purple_buddy_icon_get_account(const PurpleBuddyIcon *icon);

/**
 * purple_buddy_icon_get_username:
 * @icon: The buddy icon.
 *
 * Returns the buddy icon's username.
 *
 * Returns: The username.
 */
const char *purple_buddy_icon_get_username(const PurpleBuddyIcon *icon);

/**
 * purple_buddy_icon_get_checksum:
 * @icon: The buddy icon.
 *
 * Returns the buddy icon's checksum.
 *
 * This function is really only for protocol use.
 *
 * Returns: The checksum.
 */
const char *purple_buddy_icon_get_checksum(const PurpleBuddyIcon *icon);

/**
 * purple_buddy_icon_get_data:
 * @icon: The buddy icon.
 * @len:  If not %NULL, the length of the icon data returned will be
 *             set in the location pointed to by this.
 *
 * Returns the buddy icon's data.
 *
 * Returns: A pointer to the icon data.
 */
gconstpointer purple_buddy_icon_get_data(const PurpleBuddyIcon *icon, size_t *len);

/**
 * purple_buddy_icon_get_stream:
 * @icon: The #PurpleBuddyIcon instance.
 *
 * Gets the data of @icon as a #GInputStream.
 *
 * Returns: (transfer full): A new #GInputStream.
 *
 * Since: 3.0.0
 */
GInputStream *purple_buddy_icon_get_stream(PurpleBuddyIcon *icon);

/**
 * purple_buddy_icon_get_extension:
 * @icon: The buddy icon.
 *
 * Returns an extension corresponding to the buddy icon's file type.
 *
 * Returns: The icon's extension, "icon" if unknown, or %NULL if
 *         the image data has disappeared.
 */
const char *purple_buddy_icon_get_extension(const PurpleBuddyIcon *icon);

/**
 * purple_buddy_icon_get_full_path:
 * @icon: The buddy icon
 *
 * Returns a full path to an icon.
 *
 * If the icon has data and the file exists in the cache, this will return
 * a full path to the cache file.
 *
 * In general, it is not appropriate to be poking in the icon cache
 * directly.  If you find yourself wanting to use this function, think
 * very long and hard about it, and then don't.
 *
 * Returns: (transfer none): A full path to the file, or %NULL under various conditions.
 */
const gchar *
purple_buddy_icon_get_full_path(PurpleBuddyIcon *icon);

/**************************************************************************/
/* Buddy Icon Subsystem API                                               */
/**************************************************************************/

/**
 * purple_buddy_icons_set_for_user:
 * @account: The account the user is on.
 * @username: The username of the user.
 * @icon_data: (transfer full): The buddy icon data.
 * @icon_len: The length of the icon data.
 * @checksum: A protocol checksum from the protocol or %NULL.
 *
 * Sets a buddy icon for a user.
 */
void
purple_buddy_icons_set_for_user(PurpleAccount *account, const char *username,
                                void *icon_data, size_t icon_len,
                                const char *checksum);

/**
 * purple_buddy_icons_get_checksum_for_user:
 * @buddy: The buddy
 *
 * Returns the checksum for the buddy icon of a specified buddy.
 *
 * This avoids loading the icon image data from the cache if it's
 * not already loaded for some other reason.
 *
 * Returns: The checksum.
 */
const char *
purple_buddy_icons_get_checksum_for_user(PurpleBuddy *buddy);

/**
 * purple_buddy_icons_find:
 * @account:  The account the user is on.
 * @username: The username of the user.
 *
 * Returns the buddy icon information for a user.
 *
 * Returns: The icon (with a reference for the caller) if found, or %NULL if
 *         not found.
 */
PurpleBuddyIcon *
purple_buddy_icons_find(PurpleAccount *account, const char *username);

/**
 * purple_buddy_icons_find_account_icon:
 * @account: The account
 *
 * Returns the buddy icon image for an account.
 *
 * This function deals with loading the icon from the cache, if
 * needed, so it should be called in any case where you want the
 * appropriate icon.
 *
 * Returns: (transfer full): The account's buddy icon image.
 */
PurpleImage *
purple_buddy_icons_find_account_icon(PurpleAccount *account);

/**
 * purple_buddy_icons_set_account_icon:
 * @account:   The account for which to set a custom icon.
 * @icon_data: The image data of the icon, which the
 *                  buddy icon code will free.
 * @icon_len:  The length of the data in @icon_data.
 *
 * Sets a buddy icon for an account.
 *
 * This function will deal with saving a record of the icon,
 * caching the data, etc.
 *
 * Returns: (transfer none): The icon that was set.
 */
PurpleImage *
purple_buddy_icons_set_account_icon(PurpleAccount *account,
                                    guchar *icon_data, size_t icon_len);

/**
 * purple_buddy_icons_get_account_icon_timestamp:
 * @account: The account
 *
 * Returns the timestamp of when the icon was set.
 *
 * This is intended for use in protocols that require a timestamp for
 * buddy icon update reasons.
 *
 * Returns: The time the icon was set, or 0 if an error occurred.
 */
time_t
purple_buddy_icons_get_account_icon_timestamp(PurpleAccount *account);

/**
 * purple_buddy_icons_node_has_custom_icon:
 * @node: The blist node.
 *
 * Returns a boolean indicating if a given blist node has a custom buddy icon.
 *
 * Returns: A boolean indicating if @node has a custom buddy icon.
 */
gboolean
purple_buddy_icons_node_has_custom_icon(PurpleBlistNode *node);

/**
 * purple_buddy_icons_node_find_custom_icon:
 * @node: The node.
 *
 * Returns the custom buddy icon image for a blist node.
 *
 * This function deals with loading the icon from the cache, if
 * needed, so it should be called in any case where you want the
 * appropriate icon.
 *
 * Returns: (transfer full): The custom buddy icon.
 */
PurpleImage *
purple_buddy_icons_node_find_custom_icon(PurpleBlistNode *node);

/**
 * purple_buddy_icons_node_set_custom_icon:
 * @node:      The blist node for which to set a custom icon.
 * @icon_data: The image data of the icon, which the buddy icon code will
 *                  free. Use NULL to unset the icon.
 * @icon_len:  The length of the data in @icon_data.
 *
 * Sets a custom buddy icon for a blist node.
 *
 * This function will deal with saving a record of the icon, caching the data,
 * etc.
 *
 * Returns: (transfer none): The icon that was set.
 */
PurpleImage *
purple_buddy_icons_node_set_custom_icon(PurpleBlistNode *node,
                                        guchar *icon_data, size_t icon_len);

/**
 * purple_buddy_icons_node_set_custom_icon_from_file:
 * @node:      The blist node for which to set a custom icon.
 * @filename:  The path to the icon to set for the blist node. Use NULL
 *                  to unset the custom icon.
 *
 * Sets a custom buddy icon for a blist node.
 *
 * Convenience wrapper around purple_buddy_icons_node_set_custom_icon.
 * See purple_buddy_icons_node_set_custom_icon().
 *
 * Returns: (transfer none): The icon that was set.
 */
PurpleImage *
purple_buddy_icons_node_set_custom_icon_from_file(PurpleBlistNode *node,
                                                  const gchar *filename);

/**
 * purple_buddy_icons_set_caching:
 * @caching: TRUE if buddy icon caching should be enabled, or
 *                FALSE otherwise.
 *
 * Sets whether or not buddy icon caching is enabled.
 */
void purple_buddy_icons_set_caching(gboolean caching);

/**
 * purple_buddy_icons_is_caching:
 *
 * Returns whether or not buddy icon caching should be enabled.
 *
 * The default is TRUE, unless otherwise specified by
 * purple_buddy_icons_set_caching().
 *
 * Returns: TRUE if buddy icon caching is enabled, or FALSE otherwise.
 */
gboolean purple_buddy_icons_is_caching(void);

/**
 * purple_buddy_icons_set_cache_dir:
 * @cache_dir: The directory to store buddy icon cache files to.
 *
 * Sets the directory used to store buddy icon cache files.
 */
void purple_buddy_icons_set_cache_dir(const char *cache_dir);

/**
 * purple_buddy_icons_get_cache_dir:
 *
 * Returns the directory used to store buddy icon cache files.
 *
 * The default directory is PURPLEDIR/icons, unless otherwise specified
 * by purple_buddy_icons_set_cache_dir().
 *
 * Returns: The directory to store buddy icon cache files to.
 */
const char *purple_buddy_icons_get_cache_dir(void);

/**
 * purple_buddy_icons_get_handle:
 *
 * Returns the buddy icon subsystem handle.
 *
 * Returns: The subsystem handle.
 */
void *purple_buddy_icons_get_handle(void);

/**
 * purple_buddy_icons_init:
 *
 * Initializes the buddy icon subsystem.
 */
void purple_buddy_icons_init(void);

/**
 * purple_buddy_icons_uninit:
 *
 * Uninitializes the buddy icon subsystem.
 */
void purple_buddy_icons_uninit(void);

/**************************************************************************/
/* Buddy Icon Spec API                                                    */
/**************************************************************************/

/**
 * purple_buddy_icon_spec_get_type:
 *
 * Returns: The #GType for the #PurpleBuddyIconSpec boxed structure.
 */
GType purple_buddy_icon_spec_get_type(void);

/**
 * purple_buddy_icon_spec_new:
 * @format:        A comma-delimited list of image formats or %NULL if
 *                 icons are not supported
 * @min_width:     Minimum width of an icon
 * @min_height:    Minimum height of an icon
 * @max_width:     Maximum width of an icon
 * @max_height:    Maximum height of an icon
 * @max_filesize:  Maximum file size in bytes
 * @scale_rules:   How to stretch this icon
 *
 * Creates a new #PurpleBuddyIconSpec instance.
 *
 * Returns:  A new buddy icon spec.
 */
PurpleBuddyIconSpec *purple_buddy_icon_spec_new(char *format, int min_width,
		int min_height, int max_width, int max_height, size_t max_filesize,
		PurpleBuddyIconScaleFlags scale_rules);

/**
 * purple_buddy_icon_spec_free:
 * @spec: The #PurpleBuddyIconSpec instance.
 *
 * Frees @spec.
 *
 * Since: 3.0.0
 */
void purple_buddy_icon_spec_free(PurpleBuddyIconSpec *spec);

/**
 * purple_buddy_icon_spec_get_scaled_size:
 * @spec: The buddy icon spec.
 * @width: (inout): On input, the suggested width. On output, the width
 *                  constrained by the spec.
 * @height: (inout): On input, the suggested height. On output, the height
 *                   constrained by the spec.
 *
 * Gets display size for a buddy icon
 */
void purple_buddy_icon_spec_get_scaled_size(PurpleBuddyIconSpec *spec,
		int *width, int *height);

G_END_DECLS

#endif /* PURPLE_BUDDYICON_H */