Add pointlog2svg utility for the thesis
[numtypysics.git] / Hildon.cpp
blob8b44bb4c9c77ba5b1bcb08fe68201def00e066e9
1 /*
2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
17 #ifdef USE_HILDON
18 #include <stdio.h>
19 #include <string.h>
20 #include <glib-object.h>
21 #include <glibconfig.h>
22 #include <glib/gmacros.h>
23 #include <libosso.h>
24 #include <ossoemailinterface.h>
26 #include "Hildon.h"
27 #include "Config.h"
28 #include "happyhttp.h"
29 using namespace happyhttp;
31 #define NP_NAME "NumptyPhysics"
32 #define NP_SERVICE "org.maemo.garage.numptyphysics"
33 #define NP_OBJECT "org/maemo/garage/numptyphysics" /* / ?? */
34 #define NP_INTERFACE "org.maemo.garage.numptyphysics"
35 #define NP_VERSION "1.0"
36 #define MAX_FILES 32
39 static struct HildonState {
40 GMainContext *gcontext;
41 osso_context_t *osso;
42 int numFiles;
43 char* files[MAX_FILES];
44 FILE *httpFile;
45 int httpSize;
46 } g_state = {NULL,0};
49 static void http_begin_cb( const Response* r, void* userdata )
51 switch ( r->getstatus() ) {
52 case OK:
53 g_state.httpSize = 0;
54 break;
55 default:
56 fprintf(stderr,"http status=%d %s\n",r->getstatus(),r->getreason());
57 g_state.httpSize = -1;
58 break;
62 static void http_data_cb( const Response* r, void* userdata,
63 const unsigned char* data, int numbytes )
65 fwrite( data, 1, numbytes, g_state.httpFile );
66 g_state.httpSize += numbytes;
69 static bool http_get( const char* uri,
70 const char* file )
72 char* host = strdup(uri);
73 char* e = strchr(host,'/');
74 int port = 80;
76 g_state.httpFile = fopen( HTTP_TEMP_FILE, "wt" );
77 g_state.httpSize = -1;
79 if ( e ) {
80 *e = '\0';
82 e = strchr(host,':');
83 if ( e ) {
84 *e = '\0';
85 port=atoi(e+1);
87 e = strchr(uri,'/');
88 if ( e ) {
89 try {
90 fprintf(stderr,"http_get host=%s port=%d file=%s\n",host,port,e);
91 Connection con( host, port );
92 con.setcallbacks( http_begin_cb, http_data_cb, NULL, NULL );
93 con.request("GET",e,NULL,NULL,0);
94 while ( con.outstanding() ) {
95 fprintf(stderr,"http_get pump\n");
96 con.pump();
98 } catch ( Wobbly w ) {
99 fprintf(stderr,"http_get wobbly: %s\n",w.what());
103 fclose ( g_state.httpFile );
104 free( host );
105 return g_state.httpSize > 0;
109 static gint dbus_handler(const gchar *interface,
110 const gchar *method,
111 GArray *arguments,
112 gpointer data,
113 osso_rpc_t *retval)
115 if (arguments == NULL) {
116 return OSSO_OK;
119 if (g_ascii_strcasecmp(method, "mime_open") == 0) {
120 for(unsigned i = 0; i < arguments->len; ++i) {
121 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, i);
122 if (val.type == DBUS_TYPE_STRING && val.value.s != NULL) {
123 char *f = NULL;
124 fprintf(stderr,"hildon mime open \"%s\"\n",val.value.s);
125 if ( strncmp(val.value.s,"file://",7)==0
126 && g_state.numFiles < MAX_FILES ) {
127 f = val.value.s+7;
128 } else if ( ( strncmp(val.value.s,"http://",7)==0
129 || strncmp(val.value.s,"nptp://",7)==0 )
130 && http_get( val.value.s+7, HTTP_TEMP_FILE ) ) {
131 f = HTTP_TEMP_FILE;
133 if ( f ) {
134 if ( g_state.files[g_state.numFiles] ) {
135 g_free(g_state.files[g_state.numFiles]);
137 g_state.files[g_state.numFiles++] = g_strdup( f );
143 return OSSO_OK;
147 Hildon::Hildon()
149 if ( g_state.gcontext ) {
150 throw "gmainloop already initialised";
151 } else {
152 g_type_init();
153 g_state.gcontext = g_main_context_new();
155 if ( g_state.osso ) {
156 throw "hildon already initialised";
157 } else {
158 g_state.osso = osso_initialize(NP_NAME, NP_VERSION, FALSE, g_state.gcontext);
159 if (g_state.osso == NULL) {
160 fprintf(stderr, "Failed to initialize libosso\n");
161 return;
164 /* Set dbus handler to get mime open callbacks */
165 if ( osso_rpc_set_cb_f(g_state.osso,
166 NP_SERVICE,
167 NP_OBJECT,
168 NP_INTERFACE,
169 dbus_handler, NULL) != OSSO_OK) {
170 fprintf(stderr, "Failed to set mime callback\n");
171 return;
176 Hildon::~Hildon()
178 if ( g_state.osso ) {
179 osso_deinitialize( g_state.osso );
181 if ( g_state.gcontext ) {
182 g_main_context_unref( g_state.gcontext );
187 void Hildon::poll()
189 if ( g_main_context_iteration( g_state.gcontext, FALSE ) ) {
190 //fprintf(stderr, "Hildon::poll event!\n");
194 char *Hildon::getFile()
196 if ( g_state.numFiles > 0 ) {
197 return g_state.files[--g_state.numFiles];
199 return NULL;
202 bool Hildon::sendFile( char* to, char *file )
204 GSList *l = g_slist_append( NULL, (gpointer)file );
205 if ( l ) {
206 if ( osso_email_files_email( g_state.osso, l ) == OSSO_OK ) {
207 return true;
210 return false;
215 #endif //USE_HILDON