-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIAddSmileyController.m
120 lines (99 loc) · 3.45 KB
/
AIAddSmileyController.m
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
#import "AIAddSmileyController.h"
#import "AISmileyController.h"
#import <libpurple/smiley.h>
#import <AIUtilities/AIStringUtilities.h>
#define ADD_SMILEY_NIB_NAME @"AddCustomSmiley"
@implementation AIAddSmileyController
- (void)setSmileyPath:(NSString*) path {
smileyPath=[path retain];
smileyShortcut=nil;
}
- (PurpleSmiley*)findSmileyByChecksum: (const char*) checksum {
// purple_smileys_find_by_checksum seems bugged
PurpleSmiley* result=NULL;
GList *l;
GList *smleys = purple_smileys_get_all();
for (l = smleys; l; l = l->next) {
PurpleSmiley *smile = (PurpleSmiley *)l->data;
const char* p_checksum=purple_smiley_get_checksum(smile);
if(g_str_equal(checksum,p_checksum))
{
result=smile;
break;
}
}
g_list_free(smleys);
return result;
}
- (IBAction)ok:(id)sender {
NSString* shortcut=[textField stringValue];
const char* shortcut_s=[shortcut cStringUsingEncoding:NSUTF8StringEncoding];
PurpleSmiley* ex_smile;
if((ex_smile=purple_smileys_find_by_shortcut(shortcut_s))!=NULL) {
NSAlert* alert=[NSAlert alertWithMessageText:AILocalizedString(@"Emoticon with this shortcut already exist. Do you want to overwrite it?",nil) defaultButton:AILocalizedString(@"No",nil) alternateButton:AILocalizedString(@"Yes",nil) otherButton:nil informativeTextWithFormat:@""];
[alert setAlertStyle:NSWarningAlertStyle];
if([alert runModal]==NSAlertDefaultReturn)
{
[self close];
return;
}
purple_smiley_delete(ex_smile);
}
NSFileHandle* sml=[NSFileHandle fileHandleForReadingAtPath:smileyPath];
if(!sml) {
NSAlert* alert=[NSAlert alertWithMessageText:AILocalizedString(@"Cannot open image file!",nil) defaultButton:@"Ok" alternateButton:nil otherButton:nil informativeTextWithFormat:@""];
[alert setAlertStyle:NSWarningAlertStyle];
[alert runModal];
[self close];
return;
}
NSData* data=[sml readDataToEndOfFile];
[sml closeFile];
char* checksum=purple_util_get_image_checksum([data bytes],[data length]);
if((ex_smile=[self findSmileyByChecksum:checksum])!=NULL) {
NSAlert* alert=[NSAlert alertWithMessageText:AILocalizedString(@"This emoticon is already been added with another shortcut. Do you want to overwrite it?",nil) defaultButton:AILocalizedString(@"No",nil) alternateButton:AILocalizedString(@"Yes",nil) otherButton:nil informativeTextWithFormat:@""];
[alert setAlertStyle:NSWarningAlertStyle];
if([alert runModal]==NSAlertDefaultReturn)
{
g_free(checksum);
[self close];
return;
}
purple_smiley_delete(ex_smile);
}
g_free(checksum);
purple_smiley_new_from_file(shortcut_s,[smileyPath cStringUsingEncoding:NSUTF8StringEncoding]);
smileyShortcut=[shortcut retain];
[self close];
}
- (void)windowWillClose:(NSNotification *)notification {
[NSApp abortModal];
}
- (NSString*) smileyShortcut {
return smileyShortcut;
}
-(void) windowDidLoad {
[textLabel setStringValue: AILocalizedString(@"Enter the shortcut for the emoticon:",nil)];
[[self window] setDefaultButtonCell: [okButton cell]];
}
+ (NSString*)runAddSmiley:(NSString*) filePath {
[filePath retain];
NSString* result=nil;
AIAddSmileyController* contr=[[self alloc] initWithWindowNibName:ADD_SMILEY_NIB_NAME];
if(contr)
{
[contr setSmileyPath: filePath];
[NSApp runModalForWindow: [contr window]];
result=[contr smileyShortcut];
[contr release];
if(result)
[AISmileyController sendChangedNotification];
}
[filePath release];
return [result autorelease];
}
- (void) dealloc {
[smileyPath release];
[super dealloc];
}
@end