added delay after dbus event
[livewp] / applet / src / livewp-actor.c
1 /*vim: set sw=4 ts=4 et: */
2 /*
3  * This file is part of Live Wallpaper (livewp)
4  * 
5  * Copyright (C) 2010 Vlad Vasiliev
6  * Copyright (C) 2010 Tanya Makova
7  *       for the code
8  * 
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  * 
14  * This software is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  * 
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this software; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23 */
24 /*******************************************************************************/
25 #include "livewp-actor.h"
26
27 Actor* 
28 init_object(AWallpaperPlugin *desktop_plugin, 
29             gchar * name, 
30             gchar * filename, 
31             gint x, 
32             gint y, 
33             gint z, 
34             gint width, 
35             gint height, 
36             gboolean visible, 
37             gboolean load_image,
38             gint scale, 
39             gint opacity, 
40             void (*pfunc_change)(Actor*),
41             void (*pfunc_probability)(Actor*),
42             GPtrArray *child
43            )
44 {
45     Actor *actor = NULL;
46     actor = g_new0(Actor, 1);
47     actor->x = x;
48     actor->y = y;
49     actor->z = z;
50     actor->width = width;
51     actor->height = height;
52     actor->visible = visible;
53     actor->scale = scale;
54     actor->opacity = opacity;
55     actor->filename = g_strdup(filename);
56     actor->name = g_strdup(name);
57     actor->func_change = (gpointer)pfunc_change; 
58     actor->func_probability = (gpointer)pfunc_probability;
59     actor->child = child;
60     if (load_image)
61         create_hildon_actor(actor, desktop_plugin);
62     else 
63          actor->widget = NULL;
64     actor->time_start_animation = 0;
65     actor->duration_animation = 0;
66     return actor;
67 }
68
69 void 
70 destroy_actor(Actor *actor)
71 {
72     if (actor){
73         if (actor->child){
74             g_ptr_array_free(actor->child, TRUE);
75         }
76         if (actor->filename)
77             g_free(actor->filename);
78         if (actor->name)
79             g_free(actor->name);
80         gtk_widget_destroy(actor->widget);
81         //actor->widget = NULL;
82         g_free(actor);
83     }
84 }
85 static gint 
86 path_line(gint x0, gint x1, double t)
87 {
88     // уравниение прямой
89     return ((x1 - x0) * t + x0);
90 }
91 void
92 set_actor_scale(Actor *actor, double scalex, double scaley)
93 {
94     hildon_animation_actor_set_scale(
95             HILDON_ANIMATION_ACTOR(actor->widget), 
96             scalex, 
97             scaley
98     );
99
100 }
101
102 void 
103 set_actor_visible(Actor *actor, gboolean visible)
104 {
105     hildon_animation_actor_set_show(HILDON_ANIMATION_ACTOR(actor->widget), visible);
106 }
107
108 void
109 set_actor_position(Actor *actor, gint x, gint y, gint z, AWallpaperPlugin *desktop_plugin)
110 {
111     hildon_animation_actor_set_position_full(HILDON_ANIMATION_ACTOR (actor->widget), 
112                                              x-desktop_plugin->priv->xapplet, 
113                                              y-desktop_plugin->priv->yapplet, 
114                                              z);
115 }
116
117 int get_notify_count(gchar *notify_type)
118 {
119     sqlite3 *db = NULL;
120     sqlite3 *res = NULL;
121     gint rc = 0, result = 0;
122     gchar sql[1024];
123
124     rc = sqlite3_open("/home/user/.config/hildon-desktop/notifications.db", &db);
125     if (rc){
126         fprintf(stderr, "error open db %d %s\n", rc, sqlite3_errmsg(db));
127     }else {
128         snprintf(sql, sizeof(sql)-1, "select count(id) from notifications where icon_name='%s'", notify_type);
129         rc = sqlite3_prepare(db, sql, sizeof(sql)-1, &res, NULL);
130         if (rc != SQLITE_OK){
131             fprintf(stderr, "error prepare %d %s\n", rc, sql);
132         }
133         if (sqlite3_step(res) != SQLITE_ROW){
134             fprintf(stderr, "not sqlite_row\n");
135         }
136         result = sqlite3_column_int(res, 0);
137         //fprintf(stderr, "count missing calls = %d\n", call_count);
138         sqlite3_finalize(res);
139
140         sqlite3_close(db);
141     }
142     return result;
143 }
144 gchar * read_notification()
145 {
146     gchar *message = "";
147     gint count = 0;
148     
149     fprintf(stderr, "read notification \n");
150     count = get_notify_count("general_missed");
151     if (count > 0){
152         message = g_strdup_printf("%s: %d", _("Missed calls"), count);
153     }
154     count = get_notify_count("general_sms");
155     if (count > 0){
156         if (message){
157             message = g_strdup_printf("%s \n%s: %d", message, _("Missed sms"), count);
158         }else {
159             message = g_strdup_printf("%s: %d", _("Missed sms"), count);
160         }
161     }
162     count = get_notify_count("general_chat");
163     if (count > 0){
164         if (message){
165             message = g_strdup_printf("%s \n%s: %d", message, _("Missed chat"), count);
166         }else {
167             message = g_strdup_printf("%s: %d", _("Missed chat"), count);
168         }
169     }
170     count = get_notify_count("qgn_list_messagin");
171     if (count > 0){
172         if (message){
173             message = g_strdup_printf("%s \n%s: %d", message, _("Missed mail"), count);
174         }else {
175             message = g_strdup_printf("%s: %d", _("Missed mail"), count);
176         }
177     }
178     fprintf(stderr, "notify=%s\n", message);
179     return message;
180 }
181
182 void 
183 change_billboard(Actor * actor, AWallpaperPlugin *desktop_plugin)
184 {
185     GtkWidget *label;
186     gchar *mes = NULL, *message = NULL;
187     PangoFontDescription *pfd = NULL;
188      
189     fprintf(stderr, "change_billboard\n");   
190     if (desktop_plugin->priv->scene->notification < time(NULL)){
191         message = read_notification();
192         label = actor->image;
193         mes = g_markup_printf_escaped("<span bgcolor=\"%s\" foreground=\"%s\">%s</span>", "#FFFFFF", "#000000", 
194                                       message);
195         gtk_label_set_markup(GTK_LABEL(label), mes);
196         pfd = pango_font_description_from_string("Sans 16");
197         gtk_widget_modify_font(GTK_WIDGET(label), NULL);
198         gtk_widget_modify_font(GTK_WIDGET(label), pfd);
199         pango_font_description_free(pfd);
200
201         desktop_plugin->priv->scene->notification = FALSE;
202     }
203     actor->time_start_animation = time(NULL) + 20;    
204 }
205
206
207 void 
208 change_billboard1(Actor * actor, AWallpaperPlugin *desktop_plugin)
209 {
210     GtkWidget *label;
211     sqlite3 *db = NULL;
212     sqlite3_stmt *res = NULL;
213     gchar *errMsg = NULL, *message;
214     gchar sql[1024];
215     gint call_count=0, sms_count=0, rc=0;
216     GtkListStore *list = NULL;
217     PangoFontDescription *pfd = NULL;
218     
219     rc = sqlite3_open("/home/user/.rtcom-eventlogger/el.db", &db);
220     if (rc){
221         fprintf(stderr, "error open db %d %s\n", rc, sqlite3_errmsg(db));
222     }else {
223         snprintf(sql, sizeof(sql)-1, "select count(id) from Events where event_type_id=%d", 3);
224 //#if 0
225         rc = sqlite3_prepare(db, sql, sizeof(sql)-1, &res, NULL);
226         if (rc != SQLITE_OK){
227             fprintf(stderr, "error prepare %d %s\n", rc, sql);
228         }
229         if (sqlite3_step(res) != SQLITE_ROW){
230             fprintf(stderr, "not sqlite_row\n");
231         }
232         call_count = sqlite3_column_int(res, 0);
233         //fprintf(stderr, "count missing calls = %d\n", call_count);
234         sqlite3_finalize(res);
235
236         snprintf(sql, sizeof(sql)-1, "select count(id) from Events where event_type_id=%d and is_read=%d", 7, 0);
237         rc = sqlite3_prepare(db, sql, sizeof(sql)-1, &res, NULL);
238         if (rc != SQLITE_OK){
239             fprintf(stderr, "error prepare %d %s\n", rc, sql);
240         }
241         if (sqlite3_step(res) != SQLITE_ROW){
242             fprintf(stderr, "not sqlite_row\n");
243         }
244         sms_count = sqlite3_column_int(res, 0);
245         //fprintf(stderr, "count sms = %d\n", sms_count);
246         sqlite3_finalize(res);
247
248 //#endif
249         sqlite3_close(db);
250     }
251     label = actor->image;
252     message = g_markup_printf_escaped("<span bgcolor=\"%s\" foreground=\"%s\">Missed calls: %d Unread sms: %d</span>", "#FFFFFF", "#000000", call_count, sms_count);
253     gtk_label_set_markup(GTK_LABEL(label), message);
254     g_free(message);
255     pfd = pango_font_description_from_string("Sans 14");
256     gtk_widget_modify_font(GTK_WIDGET(label), NULL);
257     gtk_widget_modify_font(GTK_WIDGET(label), pfd);
258     pango_font_description_free(pfd);
259     actor->time_start_animation = time(NULL) + 20;    
260 }
261
262
263 void 
264 change_moon(Actor * actor, AWallpaperPlugin *desktop_plugin)
265 {
266     gint phase;
267     char *newfile;
268     gint x0 = 150,
269          x1 = 650, 
270          x, y;
271     struct timeval tvb;     
272     suseconds_t ms;
273     long sec;
274     double t;
275 #if 0
276     gint y0, y1, x2, y2;
277     double a, b, c;
278     a = (double)(y2 - (double)(x2*(y1-y0) + x1*y0 - x0*y1)/(x1-x0))/(x2*(x2-x0-x1)+x0*x1);
279     b = (double)(y1-y0)/(x1-x0) - (double)a*(x0+x1);
280     c = (double)(x1*y0 - x0*y1)/(x1-x0) + (double)a*x0*x1;
281     fprintf(stderr, "a=%f, b=%f, c=%f\n", a, b, c);
282 #endif
283     gettimeofday(&tvb, NULL);
284     
285     ms = tvb.tv_usec;
286     sec = tvb.tv_sec;
287
288     if (actor){
289         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT){
290             if (!actor->visible){
291                 actor->visible = TRUE;
292                 phase = get_moon_phase();
293                 newfile = g_strdup_printf( "%s%d.png", actor->name, phase);
294                 if (actor->filename)
295                     g_free(actor->filename);
296                 actor->filename = newfile;
297                 actor->time_start_animation = sec - fast_rnd(60 * 60);
298                 actor->duration_animation = 1 * 60 * 60;
299                 create_hildon_actor(actor, desktop_plugin);
300
301             }
302             t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
303             if (t <= 1)
304                 x = path_line(x0, x1, t);
305             else 
306                 x = path_line(x1, x0, t-1);
307             y = 0.001920*x*x - 1.536*x + 337.2;
308             //y = a*x*x + b*x + c;
309
310             set_actor_position(actor, x, y, actor->z, desktop_plugin);
311
312             if (t>=2){
313                 actor->time_start_animation = sec;
314             }
315
316          }else if (actor->visible){
317             actor->visible = FALSE;
318             fprintf(stderr, "destroy moon \n");
319             destroy_hildon_actor(actor);
320             actor->time_start_animation = 0;
321         } 
322     }
323     
324 }
325
326 void 
327 change_sun(Actor * actor, AWallpaperPlugin *desktop_plugin)
328 {
329     double alt, azm;
330     gint x, y;
331
332     //fprintf(stderr, "change sun\n");
333     if (actor){
334         if (desktop_plugin->priv->scene->daytime != TIME_NIGHT){
335             if (!actor->visible){
336                 actor->visible = TRUE;
337                 create_hildon_actor(actor, desktop_plugin);
338             }
339             get_sun_pos(&alt, &azm);
340             get_sun_screen_pos(alt, azm, &x, &y);
341             actor->x = x;
342             actor->y = y;
343             set_actor_position(actor, x, y, actor->z, desktop_plugin);
344             actor->time_start_animation = time(NULL) + 60;
345          }else if (actor->visible){
346             actor->visible = FALSE;
347             destroy_hildon_actor(actor);
348             actor->time_start_animation = 0;
349         } 
350     }
351     
352 }
353
354 void 
355 change_tram(Actor * actor, AWallpaperPlugin *desktop_plugin)
356 {
357     gint x0 = -300, y0 = 225, scale0 = 100,
358          x1 = 800, y1 = 162, scale1 = 130, 
359          x, y, scale;
360     struct timeval tvb;     
361     suseconds_t ms;
362     long sec;
363     double t;
364
365     //fprintf(stderr, "change tram\n");
366     gettimeofday(&tvb, NULL);
367     
368     ms = tvb.tv_usec;
369     sec = tvb.tv_sec;
370     
371     if (!actor->visible){
372         actor->visible = TRUE;
373         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT){
374             if (actor->filename)
375                 g_free(actor->filename);
376             actor->filename = g_strdup("tram_dark.png");
377         } else{
378             if (actor->filename)
379                 g_free(actor->filename);
380             actor->filename = g_strdup("tram.png");
381         }
382         create_hildon_actor(actor, desktop_plugin);
383     }
384     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
385     x = path_line(x0, x1, t);
386     y = path_line(y0, y1, t);
387     scale = path_line(scale0, scale1, t);
388     set_actor_position(actor, x, y, actor->z, desktop_plugin);
389     set_actor_scale(actor, (double)scale/100, (double)scale/100);
390     if (t >= 1){
391         /* stop animation */
392         actor->visible = FALSE;
393         destroy_hildon_actor(actor);
394         actor->time_start_animation = sec + fast_rnd(60);
395     }
396 }
397
398 void
399 change_plane1(Actor *actor, AWallpaperPlugin *desktop_plugin)
400 {
401     gint x0 = 620, y0 = 233,
402          x1 = 79, y1 = -146, 
403          x, y;
404     struct timeval tvb;     
405     suseconds_t ms;
406     long sec;
407     double t;
408
409     gettimeofday(&tvb, NULL);
410     
411     ms = tvb.tv_usec;
412     sec = tvb.tv_sec;
413 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
414    
415     if (desktop_plugin->priv->scene->daytime != TIME_NIGHT){
416         if (actor->time_start_animation == 0){
417             actor->time_start_animation = sec + fast_rnd(180);
418             return;
419         }
420     }
421     if (!actor->visible){
422         actor->visible = TRUE;
423         create_hildon_actor(actor, desktop_plugin);
424     }
425     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
426     x = path_line(x0, x1, t);
427     y = path_line(y0, y1, t);
428     //scale = path_line(scale0, scale1, t);
429     set_actor_position(actor, x, y, actor->z, desktop_plugin);
430     if (t >= 1){
431         /* stop animation */
432         actor->visible = FALSE;
433         destroy_hildon_actor(actor);
434         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT) 
435             actor->time_start_animation = 0;
436         else 
437             actor->time_start_animation = sec + fast_rnd(180);
438     }
439
440 }
441
442 void
443 change_plane2(Actor *actor, AWallpaperPlugin *desktop_plugin)
444 {
445     gint x0 = -actor->width, y0 = 45,
446          x1 = 800, y1 = 20, 
447          x, y;
448     struct timeval tvb;     
449     suseconds_t ms;
450     long sec;
451     double t;
452
453     gettimeofday(&tvb, NULL);
454     
455     ms = tvb.tv_usec;
456     sec = tvb.tv_sec;
457 //    fprintf(stderr, "1 %f - %d\n", sec+(double)ms/100000, now);
458     if (desktop_plugin->priv->scene->daytime != TIME_NIGHT){
459         if (actor->time_start_animation == 0){
460             actor->time_start_animation = sec + fast_rnd(180);
461             return;
462         }
463     }
464     if (!actor->visible){
465         actor->visible = TRUE;
466         create_hildon_actor(actor, desktop_plugin);
467     }
468
469     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
470     x = path_line(x0, x1, t);
471     y = path_line(y0, y1, t);
472     //scale = path_line(scale0, scale1, t);
473     set_actor_position(actor, x, y, actor->z, desktop_plugin);
474     if (t >= 1){
475         /* stop animation */
476         actor->visible = FALSE;
477         destroy_hildon_actor(actor);
478         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT) 
479             actor->time_start_animation = 0;
480         else 
481             actor->time_start_animation = sec + fast_rnd(180);
482     }
483
484 }
485
486 void
487 change_cloud(Actor *actor, AWallpaperPlugin *desktop_plugin)
488 {
489     gint x0, y0 = 300, scale0 = 100,
490          x1, y1 = -actor->height, scale1 = 150, 
491          x, y, scale;
492     struct timeval tvb;     
493     suseconds_t ms;
494     long sec;
495     double t;
496     gchar *newfile;
497
498     //fprintf(stderr, "change cloud\n");
499     gettimeofday(&tvb, NULL);
500     
501     ms = tvb.tv_usec;
502     sec = tvb.tv_sec;
503    
504     if (!actor->visible){
505         actor->visible = TRUE;
506         if (desktop_plugin->priv->scene->daytime == TIME_NIGHT){
507             newfile = g_strdup_printf("%s_dark.png", actor->name);
508         }else{
509             newfile = g_strdup_printf("%s.png", actor->name);
510         } 
511         if (actor->filename)
512             g_free(actor->filename);
513         actor->filename = newfile;
514          
515         create_hildon_actor(actor, desktop_plugin);
516     }
517     t = (double)((double)sec+(double)ms/1000000 - actor->time_start_animation) / actor->duration_animation;
518     
519     if (desktop_plugin->priv->scene->wind_orientation == 1){
520         x0 = -actor->width;
521         x1 = 800;
522     }
523     else {
524         x0 = 800;
525         x1 = -actor->width;
526     }
527
528     x = path_line(x0, x1, t);    
529     y = -desktop_plugin->priv->scene->wind_angle * (x - x0) + actor->y;
530     scale = path_line(scale0, scale1, (double)(y - y0)/(y1 - y0));
531
532     set_actor_position(actor, x, y, actor->z, desktop_plugin);
533     set_actor_scale(actor, (double)scale/100, (double)scale/100);
534     if ((y < y1 || y > y0) || t >= 1){
535         /* stop animation */
536         actor->visible = FALSE;
537         destroy_hildon_actor(actor);
538         actor->time_start_animation = sec + fast_rnd(300);
539         actor->y = fast_rnd(300);
540     }
541
542 }
543
544 void
545 change_wind(Actor *actor, AWallpaperPlugin *desktop_plugin)
546 {
547     desktop_plugin->priv->scene->wind_orientation = fast_rnd(2);
548     if (desktop_plugin->priv->scene->wind_orientation == 0) desktop_plugin->priv->scene->wind_orientation = -1;
549     desktop_plugin->priv->scene->wind_angle = (double)(fast_rnd(200) - 100) / 100;
550     actor->time_start_animation = time(NULL) + (fast_rnd(10) + 10) * 60;
551     //fprintf(stderr, "change wind orient = %d angle = %f after = %d\n", scene.wind_orientation, scene.wind_angle, actor->time_start_animation-time(NULL));
552 }
553
554 void 
555 change_window1(Actor * actor, AWallpaperPlugin *desktop_plugin)
556 {
557     gint now = time(NULL);
558     if (desktop_plugin->priv->scene->daytime == TIME_DAY){
559         if (actor->widget){
560             actor->visible = FALSE;
561             destroy_hildon_actor(actor);
562         }
563         actor->time_start_animation = 0;
564         return;
565     }else {
566         if (!actor->widget)
567             create_hildon_actor(actor, desktop_plugin);
568         if (actor->time_start_animation == 0){
569             actor->time_start_animation = now + fast_rnd(30);
570             return;
571         }
572     }
573
574     if (!actor->visible)
575         actor->visible = TRUE;
576     else 
577         actor->visible = FALSE;
578     set_actor_visible(actor, actor->visible);
579     actor->time_start_animation = now + fast_rnd(60) + 10;
580
581 }
582
583 void 
584 change_signal(Actor * actor, AWallpaperPlugin *desktop_plugin)
585 {
586     gint now = time(NULL);
587     Actor *a;
588     a = g_ptr_array_index(actor->child, 0);
589     if (a->visible)
590         a->visible = FALSE;
591     else 
592         a->visible = TRUE;
593     set_actor_visible(a, a->visible);
594     
595     a = g_ptr_array_index(actor->child, 1);
596     if (a->visible)
597         a->visible = FALSE;
598     else 
599         a->visible = TRUE;
600     set_actor_visible(a, a->visible);
601
602     actor->time_start_animation = now + fast_rnd(30) + 10;
603 }
604
605 void
606 change_layer(Actor * actor, AWallpaperPlugin *desktop_plugin)
607 {
608     gint y, speed1 = 8, speed2 = 16;
609     Actor *a;
610
611     if (!desktop_plugin->priv->rich_animation) return;
612
613     a = g_ptr_array_index(actor->child, 0);
614     y = a->y + speed1;
615     if (y > 480) y = -480;
616     set_actor_position(a, a->x, y, a->z, desktop_plugin);
617     a->y = y;
618     
619     a = g_ptr_array_index(actor->child, 1);
620     y = a->y + speed1;
621     if (y > 480) y = -480;
622     set_actor_position(a, a->x, y, a->z, desktop_plugin);
623     a->y = y;
624
625     a = g_ptr_array_index(actor->child, 2);
626     y = a->y + speed2;
627     if (y > 480) y = -480;
628     set_actor_position(a, a->x, y, a->z, desktop_plugin);
629     a->y = y;
630
631     a = g_ptr_array_index(actor->child, 3);
632     y = a->y + speed2;
633     if (y > 480) y = -480;
634     set_actor_position(a, a->x, y, a->z, desktop_plugin);
635     a->y = y;
636 }
637
638 void 
639 change_static_actor(Actor * actor, AWallpaperPlugin *desktop_plugin)
640 {
641     gchar *newfile;
642     newfile = g_strdup_printf("%s%d.png", actor->name, desktop_plugin->priv->scene->daytime); 
643     if (actor->filename)
644             g_free(actor->filename);
645     actor->filename = newfile;
646     change_hildon_actor(actor, desktop_plugin);
647 }
648
649 void 
650 change_static_actor_with_corner(Actor * actor, AWallpaperPlugin *desktop_plugin)
651 {
652     gchar buffer[2048];
653
654     if (desktop_plugin->priv->right_corner)
655         gtk_widget_destroy(desktop_plugin->priv->right_corner);
656     snprintf(buffer, sizeof(buffer) - 1, "%s/%s/town%i_right_corner.png", \
657                                   THEME_PATH, desktop_plugin->priv->theme, desktop_plugin->priv->scene->daytime);
658     desktop_plugin->priv->right_corner = gtk_image_new_from_file (buffer);
659     if (desktop_plugin->priv->right_corner){
660         gtk_fixed_put(GTK_FIXED(desktop_plugin->priv->main_widget), desktop_plugin->priv->right_corner, 0, 0);
661         gtk_widget_show (desktop_plugin->priv->right_corner);
662     }
663     change_static_actor(actor, desktop_plugin);
664
665 }