2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
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.
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.
20 #include <glib-object.h>
21 #include <glibconfig.h>
22 #include <glib/gmacros.h>
24 #include <ossoemailinterface.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"
39 static struct HildonState
{
40 GMainContext
*gcontext
;
43 char* files
[MAX_FILES
];
49 static void http_begin_cb( const Response
* r
, void* userdata
)
51 switch ( r
->getstatus() ) {
56 fprintf(stderr
,"http status=%d %s\n",r
->getstatus(),r
->getreason());
57 g_state
.httpSize
= -1;
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
,
72 char* host
= strdup(uri
);
73 char* e
= strchr(host
,'/');
76 g_state
.httpFile
= fopen( HTTP_TEMP_FILE
, "wt" );
77 g_state
.httpSize
= -1;
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");
98 } catch ( Wobbly w
) {
99 fprintf(stderr
,"http_get wobbly: %s\n",w
.what());
103 fclose ( g_state
.httpFile
);
105 return g_state
.httpSize
> 0;
109 static gint
dbus_handler(const gchar
*interface
,
115 if (arguments
== NULL
) {
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
) {
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
) {
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
) ) {
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
);
149 if ( g_state
.gcontext
) {
150 throw "gmainloop already initialised";
153 g_state
.gcontext
= g_main_context_new();
155 if ( g_state
.osso
) {
156 throw "hildon already initialised";
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");
164 /* Set dbus handler to get mime open callbacks */
165 if ( osso_rpc_set_cb_f(g_state
.osso
,
169 dbus_handler
, NULL
) != OSSO_OK
) {
170 fprintf(stderr
, "Failed to set mime callback\n");
178 if ( g_state
.osso
) {
179 osso_deinitialize( g_state
.osso
);
181 if ( g_state
.gcontext
) {
182 g_main_context_unref( g_state
.gcontext
);
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
];
202 bool Hildon::sendFile( char* to
, char *file
)
204 GSList
*l
= g_slist_append( NULL
, (gpointer
)file
);
206 if ( osso_email_files_email( g_state
.osso
, l
) == OSSO_OK
) {