Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nsapp.m « osx « src - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4e7b6bde4c558dbde67a184e2bc389c07c0ce57d (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
#import "nsapp.h"
#include <notify.h>

#include <AppKit/NSSound.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <Python.h>


#define GAJIM_POOL_ALLOC \
     NSAutoreleasePool *gajim_pool = [[NSAutoreleasePool alloc] init];
#define GAJIM_POOL_FREE [gajim_pool release];


static PyObject *netChangedCB = NULL;
static NSFileHandle* netNotifyFH = nil;
static int netNotifyToken = -1;


@implementation NSApplication (Gajim)

- (void) initGUI
{
    [NSBundle loadNibNamed:@"Gajim" owner:NSApp];
}

+ (void) netNotifyCB: (NSNotification*) notif
{
    NSLog(@"Network changed notification");

    if (netChangedCB)
    {
        PyObject_CallObject(netChangedCB, NULL);
    }

	[[notif object] readInBackgroundAndNotify];		
}

- (BOOL) initNetNotify
{
    int fd = 0;

    if (notify_register_file_descriptor(
                "com.apple.system.config.network_change", &fd, 0,
                &netNotifyToken) != NOTIFY_STATUS_OK)
    {
        return FALSE;
    }

    netNotifyFH = [[NSFileHandle alloc] initWithFileDescriptor: fd];
    [[NSNotificationCenter defaultCenter] addObserver: [self class] 
           selector: @selector(netNotifyCB:) 
           name: NSFileHandleReadCompletionNotification 
           object: netNotifyFH];
    [netNotifyFH readInBackgroundAndNotify];	

    return TRUE;
}

- (void) initGajim
{
    GAJIM_POOL_ALLOC

    [self initGUI];
    [self initNetNotify];

    [NSApp setDelegate:self];
    [NSApp finishLaunching];

    GAJIM_POOL_FREE
}

- (void) orderFrontStandardAboutPanel: (id)sender
{
    PyRun_SimpleString("\n\
import gobject\n\
import dialogs\n\
def doAbout():\n\
	dialogs.AboutDialog()\n\
	return None\n\
gobject.idle_add(doAbout)\n\
");
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
/*
    PyRun_SimpleString("\n\
import gajim\n\
import gobject\n\
def doQuit():\n\
	gajim.interface.roster.on_quit_menuitem_activate(None)\n\
	return None\n\
gobject.idle_add(doQuit)\n\
");
*/
    return NSTerminateNow;
}

- (BOOL) application:(NSApplication *)theApplication 
            openFile:(NSString *)filename
{
    NSLog(@"openFile");
    NSLog(filename);
    return YES;
}

- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
{
    NSLog(@"openFiles");

    NSEnumerator* iter = [filenames objectEnumerator];
    NSString* str;
    while ((str = [iter nextObject]))
    {
        NSLog(str);
    }
    return;
}

- (BOOL)applicationOpenUntitledFile:(NSApplication *)theApplication
{
    NSLog(@"openUntitledFile");
    return YES;
}

- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    NSLog(@"shouldOpenUntitledFile");
    return YES;
}

@end


static PyObject * nsapp_init(PyObject *self, PyObject *args)
{
    [NSApp initGajim];

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject * nsapp_setNetworkCB(PyObject *self, PyObject *args)
{
    PyArg_UnpackTuple(args, "netcb", 1, 1, &netChangedCB);

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject * nsapp_requestUserAttention(PyObject *self, PyObject *args)
{
    GAJIM_POOL_ALLOC
    [NSApp requestUserAttention:NSInformationalRequest];
    GAJIM_POOL_FREE

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject * nsapp_playFile(PyObject *self, PyObject *args)
{
    GAJIM_POOL_ALLOC

    const char* cstr = NULL;

    if (!PyArg_ParseTuple(args, "s", &cstr))
    {
        return NULL;
    }

    NSSound* snd = [[NSSound alloc] initWithContentsOfFile:
                                      [[NSString alloc] initWithUTF8String: cstr]
                                    byReference: YES];
    if (!snd)
    {
        Py_INCREF(Py_None);
        return Py_None;
    }

    if (![snd play])
    {
        Py_INCREF(Py_None);
        return Py_None;
    }

    GAJIM_POOL_FREE

	return Py_BuildValue("b", 1);
}

static PyObject * nsapp_getBundlePath(PyObject *self, PyObject *args)
{
    GAJIM_POOL_ALLOC

    NSBundle* bundle = [NSBundle mainBundle];
    if (!bundle)
    {
        Py_INCREF(Py_None);
        return Py_None;
    }

    NSString* nspath = [bundle bundlePath];
    if (!nspath)
    {
        Py_INCREF(Py_None);
        return Py_None;
    }

    const char* path = [nspath UTF8String];
    PyObject* pypath = Py_BuildValue("s", path);

    GAJIM_POOL_FREE

    return pypath;
}

static PyMethodDef nsappMethods[] =
{
	{"init", nsapp_init, METH_VARARGS, "init nsapp"},
    {"setNetworkCB", nsapp_setNetworkCB, METH_VARARGS,
     "Callback to call when the network state changes"},
    {"getBundlePath", nsapp_getBundlePath, METH_VARARGS,
     "Get the path to the bundle we were run from"},
    {"playFile", nsapp_playFile, METH_VARARGS,
     "Play a sound file"},
    {"requestUserAttention", nsapp_requestUserAttention, METH_VARARGS,
     "Sends a request for the users attention to the window manager"},
	{NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initnsapp(void)
{
    (void) Py_InitModule("nsapp", nsappMethods);
}