Main Page | Data Structures | File List | Data Fields | Globals

alarmtool.c

Go to the documentation of this file.
00001 /**
00002  * This file is part of alarmd
00003  *
00004  * Contact Person: David Weinehall <david.weinehall@nokia.com>
00005  *
00006  * Copyright (C) 2006 Nokia Corporation
00007  * alarmd and libalarm are free software; you can redistribute them
00008  * and/or modify them under the terms of the GNU Lesser General Public
00009  * License version 2.1 as published by the Free Software Foundation.
00010  *
00011  * alarmd and libalarm are distributed in the hope that they will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this software; if not, write to the Free
00018  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00019  * 02110-1301 USA
00020  */
00021 
00022 #include <getopt.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include "include/alarm_event.h"
00027 
00028 extern char *optarg;
00029 extern int optind;
00030 
00031 static int tool_add(int argc, char **argv);
00032 static int tool_remove(const char *arg);
00033 static int tool_list(int argc, char **argv);
00034 static int tool_get(const char *arg);
00035 static void usage(const char *name);
00036 
00037 int main(int argc, char **argv)
00038 {
00039         int optc;
00040         int opt_index;
00041 
00042         const char optline[] = "ar:qg:";
00043 
00044         struct option const options_top[] = {
00045                 { "add", 0, 0, 'a' },
00046                 { "remove", 1, 0, 'r' },
00047                 { "query", 0, 0, 'q' },
00048                 { "get", 1, 0, 'g' },
00049                 { 0, 0, 0, 0 }
00050         };
00051 
00052         while ((optc = getopt_long(argc, argv, optline,
00053                                         options_top, &opt_index)) != -1) {
00054                 switch (optc) {
00055                 case 'a':
00056                         return tool_add(argc, argv);
00057                         break;
00058                 case 'r':
00059                         return tool_remove(optarg);
00060                         break;
00061                 case 'q':
00062                         return tool_list(argc, argv);
00063                         break;
00064                 case 'g':
00065                         return tool_get(optarg);
00066                         break;
00067                 default:
00068                         usage(argv[0]);
00069                         return 1;
00070                 }
00071         }
00072 
00073         usage(argv[0]);
00074         return 1;
00075 
00076         return 0;
00077 }
00078 
00079 static int tool_add(int argc, char **argv)
00080 {
00081         int optc;
00082         int opt_index;
00083         alarm_event_t new_event;
00084         long event_id;
00085 
00086         memset(&new_event, 0, sizeof(new_event));
00087 
00088         struct option const options_add[] = {
00089                 { "time", 1, 0, 't' },
00090                 { "recurr", 1, 0, 'R' },
00091                 { "recurr-count", 1, 0, 'C' },
00092                 { "dbus-interface", 1, 0, 'i' },
00093                 { "dbus-service", 1, 0, 's' },
00094                 { "dbus-path", 1, 0, 'p' },
00095                 { "dbus-name", 1, 0, 'n' },
00096                 { "dbus-system-bus", 0, 0, 'y' },
00097                 { "dialog-message", 1, 0, 'm' },
00098                 { "dialog-icon", 1, 0, 'c' },
00099                 { "dialog-sound", 1, 0, 'u' },
00100                 { "exec", 1, 0, 'e' },
00101                 { "no-dialog", 0, 0, 'd' },
00102                 { "no-snooze", 0, 0, 'z' },
00103                 { "boot", 0, 0, 'o' },
00104                 { "show-icon", 0, 0, 'b' },
00105                 { "run-delayed", 0, 0, 'r' },
00106                 { "connected", 0, 0, 'w' },
00107                 { "activation", 0, 0, 'a' },
00108                 { "postpone-delayed", 0, 0, 'P' },
00109                 { "back-reschedule", 0, 0, 'B' },
00110                 { 0, 0, 0, 0 }
00111         };
00112 
00113         const char optline[] = "t:R:C:i:s:p:n:ym:c:u:e:dzobrwaPB";
00114 
00115         while ((optc = getopt_long(argc, argv, optline,
00116                                         options_add, &opt_index)) != -1) {
00117                 switch (optc) {
00118                 case 'm':
00119                         if (new_event.message) {
00120                                 free(new_event.message);
00121                         }
00122                         new_event.message = strdup(optarg);
00123                         break;
00124                 case 'c':
00125                         if (new_event.icon) {
00126                                 free(new_event.icon);
00127                         }
00128                         new_event.icon = strdup(optarg);
00129                         break;
00130                 case 'u':
00131                         if (new_event.sound) {
00132                                 free(new_event.sound);
00133                         }
00134                         new_event.sound = strdup(optarg);
00135                         break;
00136                 case 't':
00137                         new_event.alarm_time = atol(optarg);
00138                         break;
00139                 case 'R':
00140                         new_event.recurrence = atoi(optarg);
00141                         break;
00142                 case 'C':
00143                         new_event.recurrence_count = atoi(optarg);
00144                         break;
00145                 case 'i':
00146                         if (new_event.dbus_interface) {
00147                                 free(new_event.dbus_interface);
00148                         }
00149                         new_event.dbus_interface = strdup(optarg);
00150                         break;
00151                 case 's':
00152                         if (new_event.dbus_service) {
00153                                 free(new_event.dbus_service);
00154                         }
00155                         new_event.dbus_service = strdup(optarg);
00156                         break;
00157                 case 'p':
00158                         if (new_event.dbus_path) {
00159                                 free(new_event.dbus_path);
00160                         }
00161                         new_event.dbus_path = strdup(optarg);
00162                         break;
00163                 case 'n':
00164                         if (new_event.dbus_name) {
00165                                 free(new_event.dbus_name);
00166                         }
00167                         new_event.dbus_name = strdup(optarg);
00168                         break;
00169                 case 'e':
00170                         if (new_event.exec_name) {
00171                                 free(new_event.exec_name);
00172                         }
00173                         new_event.exec_name = strdup(optarg);
00174                         break;
00175                 case 'y':
00176                         new_event.flags |= ALARM_EVENT_SYSTEM;
00177                         break;
00178                 case 'd':
00179                         new_event.flags |= ALARM_EVENT_NO_DIALOG;
00180                         break;
00181                 case 'z':
00182                         new_event.flags |= ALARM_EVENT_NO_SNOOZE;
00183                         break;
00184                 case 'o':
00185                         new_event.flags |= ALARM_EVENT_BOOT;
00186                         break;
00187                 case 'b':
00188                         new_event.flags |= ALARM_EVENT_SHOW_ICON;
00189                         break;
00190                 case 'r':
00191                         new_event.flags |= ALARM_EVENT_RUN_DELAYED;
00192                         break;
00193                 case 'w':
00194                         new_event.flags |= ALARM_EVENT_CONNECTED;
00195                         break;
00196                 case 'a':
00197                         new_event.flags |= ALARM_EVENT_ACTIVATION;
00198                         break;
00199                 case 'P':
00200                         new_event.flags |= ALARM_EVENT_POSTPONE_DELAYED;
00201                         break;
00202                 case 'B':
00203                         new_event.flags |= ALARM_EVENT_BACK_RESCHEDULE;
00204                         break;
00205                 default:
00206                         fprintf(stderr, "%s: got %c\n", __FUNCTION__, optc);
00207                         break;
00208                 }
00209         }
00210         event_id = alarm_event_add(&new_event);
00211         if (event_id) {
00212                 printf("Event added with id %ld\n", event_id);
00213                 return 0;
00214         }
00215         else {
00216                 fprintf(stderr, "Adding event failed.\n");
00217                 return 1;
00218         }
00219         return 0;
00220 }
00221 
00222 static int tool_list(int argc, char **argv)
00223 {
00224         int optc;
00225         int opt_index;
00226 
00227         int32_t flag_mask = 0;
00228         int32_t flags = 0;
00229 
00230         time_t start_time = 0;
00231         time_t end_time = ~0; 
00232 
00233         long *events;
00234 
00235         if (end_time < 0) {
00236                 end_time--;
00237         }
00238 
00239         struct option const options_list[] = {
00240                 { "first-time", 1, 0, 'f' },
00241                 { "last-time", 1, 0, 'l' },
00242                 { "dbus-system-bus", 0, 0, 'y' },
00243                 { "no-dialog", 0, 0, 'd' },
00244                 { "no-snooze", 0, 0, 'z' },
00245                 { "boot", 0, 0, 'o' },
00246                 { "show-icon", 0, 0, 'b' },
00247                 { "run-delayed", 0, 0, 'r' },
00248                 { "dbus-session-bus", 0, 0, 'Y' },
00249                 { "dialog", 0, 0, 'D' },
00250                 { "snooze", 0, 0, 'Z' },
00251                 { "no-boot", 0, 0, 'O' },
00252                 { "no-show-icon", 0, 0, 'B' },
00253                 { "no-run-delayed", 0, 0, 'R' },
00254                 { "connected", 0, 0, 'c' },
00255                 { "no-connected", 0, 0, 'C' },
00256                 { "activation", 0, 0, 'a' },
00257                 { "no-activation", 0, 0, 'A' },
00258                 { "postpone-delayed", 0, 0, 'p' },
00259                 { "no-postpne-dlayed", 0, 0, 'P' },
00260                 { "back-reschedule", 0, 0, 'e' },
00261                 { "no-back-reschedule", 0, 0, 'E' },
00262                 { 0, 0, 0, 0 }
00263         };
00264 
00265         const char optline[] = "f:l:ydzobrYDZOBRcCaApPeE";
00266 
00267         while ((optc = getopt_long(argc, argv, optline,
00268                                         options_list, &opt_index)) != -1) {
00269                 switch (optc) {
00270                 case 'f':
00271                         start_time = atol(optarg);
00272                         break;
00273                 case 'l':
00274                         end_time = atol(optarg);
00275                         break;
00276                 case 'y':
00277                         flags |= ALARM_EVENT_SYSTEM;
00278                         flag_mask |= ALARM_EVENT_SYSTEM;
00279                         break;
00280                 case 'd':
00281                         flags |= ALARM_EVENT_NO_DIALOG;
00282                         flag_mask |= ALARM_EVENT_NO_DIALOG;
00283                         break;
00284                 case 'z':
00285                         flags |= ALARM_EVENT_NO_SNOOZE;
00286                         flag_mask |= ALARM_EVENT_NO_SNOOZE;
00287                         break;
00288                 case 'o':
00289                         flags |= ALARM_EVENT_BOOT;
00290                         flag_mask |= ALARM_EVENT_BOOT;
00291                         break;
00292                 case 'b':
00293                         flags |= ALARM_EVENT_SHOW_ICON;
00294                         flag_mask |= ALARM_EVENT_SHOW_ICON;
00295                         break;
00296 
00297                 case 'r':
00298                         flags |= ALARM_EVENT_RUN_DELAYED;
00299                         flag_mask |= ALARM_EVENT_RUN_DELAYED;
00300                         break;
00301                 case 'Y':
00302                         flags &= ~ALARM_EVENT_SYSTEM;
00303                         flag_mask |= ALARM_EVENT_SYSTEM;
00304                         break;
00305                 case 'D':
00306                         flags &= ~ALARM_EVENT_NO_DIALOG;
00307                         flag_mask |= ALARM_EVENT_NO_DIALOG;
00308                         break;
00309                 case 'Z':
00310                         flags &= ~ALARM_EVENT_NO_SNOOZE;
00311                         flag_mask |= ALARM_EVENT_NO_SNOOZE;
00312                         break;
00313                 case 'O':
00314                         flags &= ~ALARM_EVENT_BOOT;
00315                         flag_mask |= ALARM_EVENT_BOOT;
00316                         break;
00317                 case 'B':
00318                         flags &= ~ALARM_EVENT_SHOW_ICON;
00319                         flag_mask |= ALARM_EVENT_SHOW_ICON;
00320                         break;
00321                 case 'R':
00322                         flags &= ~ALARM_EVENT_RUN_DELAYED;
00323                         flag_mask |= ALARM_EVENT_RUN_DELAYED;
00324                         break;
00325                 case 'c':
00326                         flags |= ALARM_EVENT_CONNECTED;
00327                         flag_mask |= ALARM_EVENT_CONNECTED;
00328                         break;
00329                 case 'C':
00330                         flags &= ~ALARM_EVENT_CONNECTED;
00331                         flag_mask |= ALARM_EVENT_CONNECTED;
00332                         break;
00333                 case 'a':
00334                         flags |= ALARM_EVENT_ACTIVATION;
00335                         flag_mask |= ALARM_EVENT_ACTIVATION;
00336                         break;
00337                 case 'A':
00338                         flags &= ~ALARM_EVENT_ACTIVATION;
00339                         flag_mask |= ALARM_EVENT_ACTIVATION;
00340                         break;
00341                 case 'p':
00342                         flags |= ALARM_EVENT_POSTPONE_DELAYED;
00343                         flag_mask |= ALARM_EVENT_POSTPONE_DELAYED;
00344                         break;
00345                 case 'P':
00346                         flags &= ~ALARM_EVENT_POSTPONE_DELAYED;
00347                         flag_mask |= ALARM_EVENT_POSTPONE_DELAYED;
00348                         break;
00349                 case 'e':
00350                         flags |= ALARM_EVENT_BACK_RESCHEDULE;
00351                         flag_mask |= ALARM_EVENT_BACK_RESCHEDULE;
00352                         break;
00353                 case 'E':
00354                         flags &= ~ALARM_EVENT_BACK_RESCHEDULE;
00355                         flag_mask |= ALARM_EVENT_BACK_RESCHEDULE;
00356                         break;
00357                 default:
00358                         return 1;
00359                         break;
00360                 }
00361         }
00362 
00363         events = alarm_event_query(start_time, end_time, flag_mask, flags);
00364 
00365         if (events == NULL) {
00366                 fprintf(stderr, "Error while querying the events.\n");
00367                 return -1;
00368         } else {
00369                 if (*events == 0) {
00370                         printf("No events.\n");
00371                 } else {
00372                         unsigned int i = 0;
00373 
00374                         printf("Events found:\n");
00375                         for (i = 0; events[i] != 0; i++) {
00376                                 printf("\t%ld\n", events[i]);
00377                         }
00378                 }
00379                 free(events);
00380         }
00381 
00382         return 0;
00383 }
00384 
00385 static int tool_remove(const char *arg)
00386 {
00387         char* err;
00388         cookie_t event_id=strtol(arg,&err,10);
00389 
00390         if (*err != '\0') {
00391                 // Invalid cookie
00392                 fprintf(stderr,"Invalid event id \"%s\"\n",arg);
00393                 return 1;
00394         }
00395 
00396         if (alarm_event_del(event_id)) {
00397                 // event removed
00398                 return 0;
00399         } else {
00400                 fprintf(stderr,"Error while removing event \"%s\"\n",arg);
00401                 // Error while removing the event
00402         }
00403         return -1;
00404 }
00405 
00406 static int tool_get(const char *arg)
00407 {
00408         alarm_event_t* event;
00409         cookie_t event_id;
00410         char* err;
00411         fprintf(stderr, "%s(%s)\n", __FUNCTION__, arg);
00412 
00413         event_id=strtol(arg,&err,10);
00414 
00415         if (*err != '\0') {
00416                 fprintf(stderr,"Invalid event id \"%s\"\n",arg);
00417                 // Invalid cookie
00418                 return 1;
00419         }
00420 
00421         event = alarm_event_get(event_id);
00422 
00423         if (event==NULL) {
00424                 fprintf(stderr, "Can't get event \"%s\"\n", arg);
00425                 // Error
00426 
00427                 return -1;
00428         } else {
00429                 const char *system, *dialog, *snooze, *boot, *actdead,
00430                         *icon, *delayed, *connected, *activation,
00431                         *postpone, *breschedule;
00432 
00433                 if (event->flags & ALARM_EVENT_SYSTEM) {
00434                         system = "System ";
00435                 } else {
00436                         system = "";
00437                 }
00438 
00439                 if (event->flags & ALARM_EVENT_NO_DIALOG) {
00440                         dialog = "No_dialog ";
00441                 } else {
00442                         dialog = "";
00443                 }
00444 
00445                 if (event->flags & ALARM_EVENT_NO_SNOOZE) {
00446                         snooze = "No_snooze ";
00447                 } else {
00448                         snooze = "";
00449                 }
00450 
00451                 if (event->flags & ALARM_EVENT_BOOT) {
00452                         boot = "Boot ";
00453                 } else {
00454                         boot = "";
00455                 }         
00456 
00457                 if (event->flags & ALARM_EVENT_ACTDEAD) {
00458                         actdead = "Boot_Actdead ";
00459                 } else {
00460                         actdead = "";
00461                 }
00462 
00463                 if (event->flags & ALARM_EVENT_SHOW_ICON) {
00464                         icon = "Icon ";
00465                 } else {
00466                         icon = "";
00467                 }
00468 
00469                 if (event->flags & ALARM_EVENT_RUN_DELAYED) {
00470                         delayed = "No_dialog ";
00471                 } else {
00472                         delayed = "";
00473                 }
00474 
00475                 if (event->flags & ALARM_EVENT_CONNECTED) {
00476                         connected = "Connected ";
00477                 } else {
00478                         connected = "";
00479                 }
00480 
00481                 if (event->flags & ALARM_EVENT_ACTIVATION) {
00482                         activation = "Activation ";
00483                 } else {
00484                         activation = "";
00485                 }
00486 
00487                 if (event->flags & ALARM_EVENT_POSTPONE_DELAYED) {
00488                         postpone = "Postpone ";
00489                 } else {
00490                         postpone = "";
00491                 }
00492 
00493                 if (event->flags & ALARM_EVENT_BACK_RESCHEDULE) {
00494                         breschedule = "Back_Reschedule ";
00495                 } else {
00496                         breschedule = "";
00497                 }
00498 
00499                 printf("\ttime\t: \"%u\"\n"
00500                                 "\tmgs\t: \"%s\"\n"
00501                                 "\ticon\t: \"%s\"\n"
00502                                 "\tsound\t: \"%s\"\n"
00503                                 "\tiface\t: \"%s\"\n"
00504                                 "\tservice\t: \"%s\"\n"
00505                                 "\tpath\t: \"%s\"\n"
00506                                 "\tname\t: \"%s\"\n"
00507                                 "\texec-name\t: \"%s\"\n"
00508                                 "\tflags\t: \"%s%s%s%s%s%s%s%s%s%s%s\"\n"
00509                                 ,
00510                                 (unsigned)event->alarm_time,
00511                                 event->message,
00512                                 event->icon,
00513                                 event->sound,
00514                                 event->dbus_interface,
00515                                 event->dbus_service,
00516                                 event->dbus_path,
00517                                 event->dbus_name,
00518                                 event->exec_name,
00519                                 dialog, snooze, system, boot, actdead, icon,
00520                                 delayed, connected, activation, postpone,
00521                                 breschedule
00522                       );
00523 
00524         }
00525         return 0;
00526 }
00527 
00528 static void usage(const char *name)
00529 {
00530         printf("Usage:\n"
00531                         "\t%s -a [-tispnymcuedzobr] [option params]\n"
00532                         "\t%s -r <cookie>\n"
00533                         "\t%s -l [-ydzobrYDZOBR]\n"
00534                         "\t%s -g <cookie>\n\n"
00535                         "\t-a, --add\tAdd event to queue\n"
00536                         "\t-t, --time\n", name, name, name, name);
00537 }

Generated on Thu Dec 21 18:23:30 2006 for Alarmd by  doxygen 1.4.2