aboutsummaryrefslogtreecommitdiffstats
path: root/src/empathy-migrate-butterfly-logs.c
blob: 4a8bf973ce2597094cb38162c1ad8678c3e96b37 (plain) (blame)
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
/*
*  Copyright (C) 2010 Collabora Ltd.
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <string.h>

#include <gio/gio.h>

#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include <libempathy/empathy-debug.h>
#include <libempathy/empathy-gsettings.h>

#include <telepathy-glib/account-manager.h>
#include <telepathy-glib/util.h>
#include <telepathy-glib/defs.h>

#include "empathy-migrate-butterfly-logs.h"

static guint butterfly_log_migration_id = 0;

static void
migrate_log_files_in_dir (const gchar *dirname)
{
  GDir *dir;
  const gchar *subdir;
  gchar *new_name;
  gchar *full_path;
  GError *error = NULL;

  dir = g_dir_open (dirname, 0, &error);

  if (dir == NULL)
    {
      DEBUG ("Failed to open dir: %s", error->message);
      g_error_free (error);
      return;
    }

  while ((subdir = g_dir_read_name (dir)) != NULL)
    {
      GFile *old_gfile, *new_gfile;

      if (!tp_strdiff (subdir, "chatrooms"))
        continue;

      if (g_str_has_suffix (subdir, "#1"))
        {
          new_name = g_strndup (subdir, (strlen (subdir) - 2));
        }
      else if (g_str_has_suffix (subdir, "#32"))
        {
          gchar *tmp;
          tmp = g_strndup (subdir, (strlen (subdir) - 3));
          new_name = g_strdup_printf ("%s#yahoo", tmp);
          g_free (tmp);
        }
      else
        {
          continue;
        }

      full_path = g_build_filename (dirname, subdir, NULL);
      old_gfile = g_file_new_for_path (full_path);
      g_free (full_path);

      full_path = g_build_filename (dirname, new_name, NULL);
      new_gfile = g_file_new_for_path (full_path);
      g_free (full_path);

      if (!g_file_move (old_gfile, new_gfile, G_FILE_COPY_NONE,
              NULL, NULL, NULL, &error))
        {
          DEBUG ("Failed to move file: %s", error->message);
          g_clear_error (&error);
        }
      else
        {
          DEBUG ("Successfully migrated logs for %s", new_name);
        }

      g_free (new_name);
      g_object_unref (old_gfile);
      g_object_unref (new_gfile);
    }

  g_dir_close (dir);
}

/* This is copied from empathy-log-store-empathy.c (see #613437) */
static gchar *
log_store_account_to_dirname (TpAccount *account)
{
  const gchar *name;

  name = tp_proxy_get_object_path (account);
  if (g_str_has_prefix (name, TP_ACCOUNT_OBJECT_PATH_BASE))
    name += strlen (TP_ACCOUNT_OBJECT_PATH_BASE);

  return g_strdelimit (g_strdup (name), "/", '_');
}

static gchar *
get_log_dir_for_account (TpAccount *account)
{
  gchar *basedir;
  gchar *escaped;

  escaped = log_store_account_to_dirname (account);

  basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
    PACKAGE_NAME, "logs", escaped, NULL);

  g_free (escaped);

  return basedir;
}


static void
migration_account_manager_prepared_cb (GObject *source_object,
    GAsyncResult *result,
    gpointer user_data)
{
  TpAccountManager *am = TP_ACCOUNT_MANAGER (source_object);
  GError *error = NULL;
  GList *accounts, *l;
  GSettings *gsettings;

  if (!tp_account_manager_prepare_finish (am, result, &error))
    {
      DEBUG ("Failed to prepare the account manager: %s", error->message);
      g_error_free (error);
      return;
    }

  accounts = tp_account_manager_get_valid_accounts (am);

  for (l = accounts; l != NULL; l = l->next)
    {
      TpAccount *account = TP_ACCOUNT (l->data);
      gchar *dir, *cm;

      tp_account_parse_object_path (tp_proxy_get_object_path (account),
          &cm, NULL, NULL, NULL);

      if (tp_strdiff (cm, "butterfly"))
        {
          g_free (cm);
          continue;
        }

      dir = get_log_dir_for_account (account);
      DEBUG ("Migrating all logs from dir: %s", dir);

      migrate_log_files_in_dir (dir);

      g_free (cm);
      g_free (dir);
    }

  DEBUG ("Finished all migrating");

  gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
  g_settings_set_boolean (gsettings, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED,
      TRUE);
  g_object_unref (gsettings);

  g_list_free (accounts);
}

static gboolean
migrate_logs (gpointer data)
{
  TpAccountManager *account_manager;

  account_manager = tp_account_manager_dup ();

  tp_account_manager_prepare_async (account_manager, NULL,
      migration_account_manager_prepared_cb, NULL);

  g_object_unref (account_manager);

  return FALSE;
}

gboolean
empathy_migrate_butterfly_logs (EmpathyContact *contact)
{
  GSettings *gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
  gchar *cm;

  /* Already in progress. */
  if (butterfly_log_migration_id != 0)
    return FALSE;

  /* Already done. */
  if (g_settings_get_boolean (gsettings, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED))
    return FALSE;

  tp_account_parse_object_path (
      tp_proxy_get_object_path (empathy_contact_get_account (contact)),
      &cm, NULL, NULL, NULL);

  if (tp_strdiff (cm, "butterfly"))
    {
      g_free (cm);
      return TRUE;
    }
  g_free (cm);

  if (g_str_has_suffix (empathy_contact_get_id (contact), "#32")
      || g_str_has_suffix (empathy_contact_get_id (contact), "#1"))
    return TRUE;

  /* Okay, we know a new butterfly is being used, so we should migrate its logs */
  butterfly_log_migration_id = g_idle_add_full (G_PRIORITY_LOW,
      migrate_logs, NULL, NULL);

  return FALSE;
}