Back to index

indicator-appmenu  12.10.0
dump-app-info.c
Go to the documentation of this file.
00001 /*
00002 Prints out application info for debugging and CLI tools
00003 
00004 Copyright 2011 Canonical Ltd.
00005 
00006 Authors:
00007     Ted Gould <ted@canonical.com>
00008 
00009 This program is free software: you can redistribute it and/or modify it 
00010 under the terms of the GNU General Public License version 3, as published 
00011 by the Free Software Foundation.
00012 
00013 This program is distributed in the hope that it will be useful, but 
00014 WITHOUT ANY WARRANTY; without even the implied warranties of 
00015 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
00016 PURPOSE.  See the GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License along 
00019 with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #include "dump-app-info.h"
00023 #include "shared-values.h"
00024 
00025 typedef enum _tree_type_t tree_type_t;
00026 enum _tree_type_t {
00027        MENU_TYPE,
00028        ITEM_TYPE
00029 };
00030 
00031 typedef struct _menu_t menu_t;
00032 struct _menu_t {
00033        tree_type_t tree_type;
00034        gchar * name;
00035        int count;
00036        GList * subitems;
00037 };
00038 
00039 GList *
00040 place_on_tree (GList * tree_in, gchar ** entries)
00041 {
00042        menu_t * menu;
00043 
00044        if (entries[0] == NULL) {
00045               return tree_in;
00046        }
00047 
00048        GList * entry = tree_in;
00049        while (entry != NULL) {
00050               menu = (menu_t *)entry->data;
00051 
00052               if (g_strcmp0(menu->name, entries[0]) == 0) {
00053                      break;
00054               }
00055 
00056               entry = g_list_next(entry);
00057        }
00058 
00059        if (entry != NULL) {
00060               if (menu->tree_type == ITEM_TYPE) {
00061                      if (entries[1] != NULL) {
00062                             g_warning("Error parsing on entry '%s'", entries[0]);
00063                      } else {
00064                             menu->count++;
00065                      }
00066               } else {
00067                      menu->subitems = place_on_tree(menu->subitems, &entries[1]);
00068               }
00069 
00070               return tree_in;
00071        }
00072 
00073        menu = g_new0(menu_t, 1);
00074        menu->name = g_strdup(entries[0]);
00075 
00076        if (entries[1] == NULL) {
00077               /* This is an item */
00078               menu->tree_type = ITEM_TYPE;
00079               menu->count = 1;
00080               menu->subitems = NULL;
00081        } else {
00082               /* This is a menu */
00083               menu->tree_type = MENU_TYPE;
00084               menu->subitems = place_on_tree(NULL, &entries[1]);
00085        }
00086 
00087        return g_list_append(tree_in, menu);
00088 }
00089 
00090 static int
00091 entry_cb (void * user_data, int columns, char ** values, char ** names)
00092 {
00093        GList ** tree = (GList **)user_data;
00094 
00095        gchar ** entries = g_strsplit(values[0], DB_SEPARATOR, -1);
00096 
00097        *tree = place_on_tree(*tree, entries);
00098 
00099        g_strfreev(entries);
00100 
00101        return SQLITE_OK;
00102 }
00103 
00104 void
00105 print_tree (GList * tree, guint tab_depth)
00106 {
00107        if (tree == NULL) {
00108               return;
00109        }
00110 
00111        int i;
00112        for (i = 0; i < tab_depth; i++) {
00113               g_print("\t");
00114        }
00115 
00116        menu_t * menu = (menu_t *)tree->data;
00117 
00118        if (menu->tree_type == ITEM_TYPE) {
00119               g_print("<item name=\"%s\" count=\"%d\" />\n", menu->name, menu->count);
00120        } else {
00121               g_print("<menu name=\"%s\">\n", menu->name);
00122 
00123               GList * subs = menu->subitems;
00124               while (subs != NULL) {
00125                      print_tree(subs, tab_depth + 1);
00126                      subs = g_list_next(subs);
00127               }
00128 
00129               for (i = 0; i < tab_depth; i++) {
00130                      g_print("\t");
00131               }
00132               g_print("</menu>\n");
00133        }
00134 
00135        return;
00136 }
00137 
00138 void
00139 dump_app_info (const gchar * app, const gchar * domain, sqlite3 * db)
00140 {
00141        g_return_if_fail(app != NULL);
00142        g_return_if_fail(db != NULL);
00143 
00144        g_print("<hudappinfo>\n");
00145 
00146        g_print("\t<desktopfile path=\"%s\" />\n", app);
00147 
00148        if (domain != NULL) {
00149               g_print("\t<gettext-domain>%s</gettext-domain>\n", domain);
00150        }
00151 
00152        gchar * statement = g_strdup_printf("select entry from usage where application = '%s';", app);
00153 
00154        int exec_status = SQLITE_OK;
00155        gchar * failstring = NULL;
00156        GList * tree = NULL;
00157        exec_status = sqlite3_exec(db,
00158                                   statement,
00159                                   entry_cb, &tree, &failstring);
00160        if (exec_status != SQLITE_OK) {
00161               g_warning("Unable to get entries: %s", failstring);
00162        }
00163 
00164        g_free(statement);
00165 
00166        if (tree != NULL) {
00167               g_print("\t<menus>\n");
00168               print_tree(tree, 2);
00169               g_print("\t</menus>\n");
00170        }
00171 
00172        g_print("</hudappinfo>\n");
00173        return;
00174 }