~robertcarr/libunity-webapps/fix-1059846-happy-october

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "unity-webapps-url-db.h"
#include "unity-webapps-dirs.h"
#include "unity-webapps-debug.h"

#include <sqlite3.h>

struct _UnityWebappsUrlDB {
  sqlite3 *db;
};


#define CREATE_URL_TABLE_SQL "CREATE TABLE IF NOT EXISTS urls(url TEXT, package TEXT, PRIMARY KEY(url));"

#define INSERT_URL_SQL_TEMPLATE "INSERT OR REPLACE INTO urls(url, package) VALUES (%Q, %Q);"

#define LOOKUP_URLS_SQL_TEMPLATE "SELECT package FROM urls WHERE %Q GLOB url;"

#define UNITY_WEBAPPS_URL_DB_DEFAULT_PATH_ENV_VARIABLE "UNITY_WEBAPPS_URL_DB_DEFAULT_PATH"

static sqlite3_stmt *
unity_webapps_url_db_insert_url_prepare_statement (UnityWebappsUrlDB *url_db,
					    const gchar *url,
					    const gchar *package)
{
  gchar *statement_sql, *stripped_url, *stripped_package;
  sqlite3_stmt *statement;
  gint rc;
  
  stripped_url = g_strdup(url);
  stripped_url = g_strstrip (stripped_url);
  stripped_package = g_strdup(package);
  stripped_package = g_strstrip(stripped_package);

  statement_sql = sqlite3_mprintf (INSERT_URL_SQL_TEMPLATE, stripped_url,
				   stripped_package);
  
  statement = NULL;
  
  rc = sqlite3_prepare_v2 (url_db->db, statement_sql, -1, &statement, NULL);
  
  if (rc != SQLITE_OK)
    {
      UNITY_WEBAPPS_NOTE (URL_DB, "Failed to compile URL DB insert URL statement: %s",
			  sqlite3_errmsg (url_db->db));
    }
  sqlite3_free (statement_sql);
  
  g_free (stripped_url);
  g_free (stripped_package);
  
  return statement;
}

static sqlite3_stmt *
unity_webapps_url_db_lookup_urls_prepare_statement (UnityWebappsUrlDB *url_db,
					     const gchar *url)
{
  gchar *statement_sql;
  sqlite3_stmt *statement;
  gint rc;
  
  statement_sql = sqlite3_mprintf (LOOKUP_URLS_SQL_TEMPLATE, url);
  
  statement = NULL;
  
  rc = sqlite3_prepare_v2 (url_db->db, statement_sql, -1, &statement, NULL);
  
  if (rc != SQLITE_OK)
    {
      UNITY_WEBAPPS_NOTE (URL_DB, "Failed to compile URL DB lookup URLs statement: %s",
		  sqlite3_errmsg (url_db->db));
    }
  sqlite3_free (statement_sql);
  
  return statement;
}

gboolean
unity_webapps_url_db_lookup_urls (UnityWebappsUrlDB *url_db, const gchar *url,
				  GList **out_records)
{
  GList *records;
  sqlite3_stmt *url_statement;
  int rc;
  gboolean ret = TRUE;
  
  *out_records = records = NULL;
  
  url_statement = unity_webapps_url_db_lookup_urls_prepare_statement (url_db, url);
  
  if (url_statement == NULL)
    {
      return FALSE;
    }
  
  do {
    const gchar *package_name;
    rc = sqlite3_step (url_statement);
    
    if (rc != SQLITE_ROW)
      {
	continue;
      }
    
    package_name = (const gchar *)sqlite3_column_text (url_statement, 0);
    // TODO Hack
    records = g_list_append (records, unity_webapps_url_db_record_new (package_name, "Foo"));
  } while (rc == SQLITE_ROW);
  
  if (rc != SQLITE_DONE)
    {
      ret = FALSE;
    }
  
  sqlite3_finalize (url_statement);
  *out_records = records;
  
  return ret;
}

gboolean
unity_webapps_url_db_insert_url (UnityWebappsUrlDB *url_db, const gchar *url,
				 UnityWebappsUrlDBRecord *record)
{
  sqlite3_stmt *url_statement;
  int rc;
  gboolean ret = TRUE;
  
  url_statement = unity_webapps_url_db_insert_url_prepare_statement (url_db, url, record->package_name);
  
  if (url_statement == NULL)
    {
      return FALSE;
    }
  
  rc = sqlite3_step (url_statement);
  
  if (rc != SQLITE_DONE)
    {
      g_warning ("Error updating url in url db: %s", sqlite3_errmsg (url_db->db));
      ret = FALSE;
      
      goto out;
    }
  
 out:
  sqlite3_finalize (url_statement);
  return ret;
}

static int
unity_webapps_url_db_create_tables (UnityWebappsUrlDB *url_db)
{
  int rc;
  gchar *error_message;
  
  error_message = NULL;
  
  rc = sqlite3_exec (url_db->db, CREATE_URL_TABLE_SQL, NULL, 0, &error_message);
  
  if (rc != SQLITE_OK)
    {
      UNITY_WEBAPPS_NOTE (URL_DB, "Error creating URL db table: %s\n", error_message);
      sqlite3_free (error_message);
    }
  return rc;
}

UnityWebappsUrlDB *
unity_webapps_url_db_open (gboolean read_only, const gchar *database_path)
{
  UnityWebappsUrlDB *url_db;
  int rc;
  
  url_db = g_malloc0 (sizeof (UnityWebappsUrlDB));
  
  rc = sqlite3_open (database_path, &url_db->db);
  
  if (rc != 0)
    {
      UNITY_WEBAPPS_NOTE (URL_DB, "Failed to open app url database : %s \n", sqlite3_errmsg(url_db->db));
      sqlite3_close (url_db->db);
      g_free (url_db);
      
      return NULL;
    }
  
  rc = unity_webapps_url_db_create_tables (url_db);
  
  if (rc != 0)
    {
      sqlite3_close (url_db->db);
      g_free (url_db);
      return NULL;
    }
  
  return url_db;
}

void
unity_webapps_url_db_free (UnityWebappsUrlDB *url_db)
{
  sqlite3_close (url_db->db);
  g_free (url_db);
}

UnityWebappsUrlDB *
unity_webapps_url_db_open_default (gboolean read_only)
{
  UnityWebappsUrlDB *default_db;
  const gchar *uwa_user_dir;
  const gchar *env_db_path;
  gchar *db_path;
  
  env_db_path = g_getenv (UNITY_WEBAPPS_URL_DB_DEFAULT_PATH_ENV_VARIABLE);
  if (env_db_path != NULL)
    {
      default_db = unity_webapps_url_db_open (read_only, env_db_path);
      return default_db;
    }
  uwa_user_dir = unity_webapps_dirs_get_user_dir ();
  db_path = g_build_filename (uwa_user_dir, "availableapps-v2.db", NULL);

  default_db = unity_webapps_url_db_open (read_only, db_path);
  g_free (db_path);
  
  return default_db;
}

UnityWebappsUrlDBRecord *
unity_webapps_url_db_record_new (const gchar *package_name, const gchar *application_name)
{
  UnityWebappsUrlDBRecord *record;
  
  record = g_malloc0 (sizeof (UnityWebappsUrlDBRecord));
  record->package_name = g_strdup (package_name);
  record->application_name = g_strdup (application_name);
  
  return record;
}

void
unity_webapps_url_db_record_free (UnityWebappsUrlDBRecord *record)
{
  g_free (record->package_name);
  g_free (record->application_name);
  
  g_free (record);
}

void
unity_webapps_url_db_record_list_free (GList *record_list)
{
  GList *w;
  
  for (w = record_list; w != NULL; w = w->next)
    {
      UnityWebappsUrlDBRecord *record;
      
      record = (UnityWebappsUrlDBRecord *)w->data;
      unity_webapps_url_db_record_free (record);
    }
  
  g_list_free (record_list);
}