~ubuntu-branches/ubuntu/quantal/thunar/quantal

« back to all changes in this revision

Viewing changes to thunar/thunar-file.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc
  • Date: 2011-01-30 20:05:10 UTC
  • mfrom: (1.1.31 upstream)
  • Revision ID: james.westby@ubuntu.com-20110130200510-r0mwai90pd8in8zr
Tags: 1.2.1-0ubuntu1
New upstream bugfix release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id$ */
 
1
/* vi:set et ai sw=2 sts=2 ts=2: */
2
2
/*-
3
3
 * Copyright (c) 2005-2007 Benedikt Meurer <benny@xfce.org>
4
 
 * Copyright (c) 2009-2010 Jannis Pohlmann <jannis@xfce.org>
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify it
7
 
 * under the terms of the GNU General Public License as published by the Free
8
 
 * Software Foundation; either version 2 of the License, or (at your option)
9
 
 * any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14
 
 * more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License along with
17
 
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18
 
 * Place, Suite 330, Boston, MA  02111-1307  USA
 
4
 * Copyright (c) 2009-2011 Jannis Pohlmann <jannis@xfce.org>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or 
 
7
 * modify it under the terms of the GNU General Public License as
 
8
 * published by the Free Software Foundation; either version 2 of 
 
9
 * the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public 
 
17
 * License along with this program; if not, write to the Free 
 
18
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
 * Boston, MA 02110-1301, USA.
19
20
 */
20
21
 
21
22
#ifdef HAVE_CONFIG_H
1253
1254
                    gboolean      called_from_job,
1254
1255
                    GError      **error)
1255
1256
{
1256
 
  GFile *previous_file;
1257
 
  GFile *renamed_file;
1258
 
  gint   watch_count;
 
1257
  GKeyFile *key_file;
 
1258
  GError   *err = NULL;
 
1259
  GFile    *previous_file;
 
1260
  GFile    *renamed_file;
 
1261
  gint      watch_count;
1259
1262
 
1260
1263
  _thunar_return_val_if_fail (THUNAR_IS_FILE (file), FALSE);
1261
1264
  _thunar_return_val_if_fail (g_utf8_validate (name, -1, NULL), FALSE);
1262
1265
  _thunar_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
1263
1266
  _thunar_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1264
1267
 
1265
 
  /* remember the previous file */
1266
 
  previous_file = g_object_ref (file->gfile);
1267
 
  
1268
 
  /* try to rename the file */
1269
 
  renamed_file = g_file_set_display_name (file->gfile, name, cancellable, error);
1270
 
 
1271
 
  /* check if we succeeded */
1272
 
  if (renamed_file != NULL)
 
1268
  /* check if this file is a desktop entry */
 
1269
  if (thunar_file_is_desktop_file (file))
1273
1270
    {
1274
 
      /* set the new file */
1275
 
      file->gfile = renamed_file;
1276
 
 
1277
 
      /* reload file information */
1278
 
      thunar_file_load (file, NULL, NULL);
1279
 
 
1280
 
      /* need to re-register the monitor handle for the new uri */
1281
 
      watch_count = THUNAR_FILE_GET_WATCH_COUNT (file);
1282
 
      if (G_LIKELY (watch_count > 0))
1283
 
        {
1284
 
          /* drop the watch_count temporary */
1285
 
          THUNAR_FILE_SET_WATCH_COUNT (file, 1);
1286
 
 
1287
 
          /* drop the previous handle (with the old path) */
1288
 
          thunar_file_unwatch (file);
1289
 
 
1290
 
          /* register the new handle (with the new path) */
1291
 
          thunar_file_watch (file);
1292
 
 
1293
 
          /* reset the watch count */
1294
 
          THUNAR_FILE_SET_WATCH_COUNT (file, watch_count);
1295
 
        }
1296
 
 
1297
 
      G_LOCK (file_cache_mutex);
1298
 
 
1299
 
      /* drop the previous entry from the cache */
1300
 
      g_hash_table_remove (file_cache, previous_file);
1301
 
 
1302
 
      /* drop the reference on the previous file */
1303
 
      g_object_unref (previous_file);
1304
 
 
1305
 
      /* insert the new entry */
1306
 
      g_hash_table_insert (file_cache, g_object_ref (file->gfile), file);
1307
 
 
1308
 
      G_UNLOCK (file_cache_mutex);
1309
 
 
1310
 
      if (!called_from_job)
1311
 
        {
1312
 
          /* tell the associated folder that the file was renamed */
1313
 
          thunarx_file_info_renamed (THUNARX_FILE_INFO (file));
1314
 
 
1315
 
          /* emit the file changed signal */
1316
 
          thunar_file_changed (file);
1317
 
        }
1318
 
 
1319
 
      return TRUE;
 
1271
      /* try to load the desktop entry into a key file */
 
1272
      key_file = thunar_g_file_query_key_file (file->gfile, cancellable, &err);
 
1273
      if (key_file == NULL)
 
1274
        {
 
1275
          g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL,
 
1276
                       _("Failed to parse the desktop file: %s"), err->message);
 
1277
          g_error_free (err);
 
1278
          return FALSE;
 
1279
        }
 
1280
 
 
1281
      /* change the Name field of the desktop entry */
 
1282
      g_key_file_set_string (key_file, G_KEY_FILE_DESKTOP_GROUP,
 
1283
                             G_KEY_FILE_DESKTOP_KEY_NAME, name);
 
1284
 
 
1285
      /* write the changes back to the file */
 
1286
      if (thunar_g_file_write_key_file (file->gfile, key_file, cancellable, &err))
 
1287
        {
 
1288
          /* reload file information */
 
1289
          thunar_file_load (file, NULL, NULL);
 
1290
 
 
1291
          if (!called_from_job)
 
1292
            {
 
1293
              /* tell the associated folder that the file was renamed */
 
1294
              thunarx_file_info_renamed (THUNARX_FILE_INFO (file));
 
1295
 
 
1296
              /* notify everybody that the file has changed */
 
1297
              thunar_file_changed (file);
 
1298
            }
 
1299
 
 
1300
          /* release the key file and return with success */
 
1301
          g_key_file_free (key_file);
 
1302
          return TRUE;
 
1303
        }
 
1304
      else
 
1305
        {
 
1306
          /* propagate the error message and return with failure */
 
1307
          g_propagate_error (error, err);
 
1308
          g_key_file_free (key_file);
 
1309
          return FALSE;
 
1310
        }
1320
1311
    }
1321
1312
  else
1322
1313
    {
1323
 
      g_object_unref (previous_file);
1324
 
 
1325
 
      return FALSE;
 
1314
      /* remember the previous file */
 
1315
      previous_file = g_object_ref (file->gfile);
 
1316
      
 
1317
      /* try to rename the file */
 
1318
      renamed_file = g_file_set_display_name (file->gfile, name, cancellable, error);
 
1319
 
 
1320
      /* check if we succeeded */
 
1321
      if (renamed_file != NULL)
 
1322
        {
 
1323
          /* set the new file */
 
1324
          file->gfile = renamed_file;
 
1325
 
 
1326
          /* reload file information */
 
1327
          thunar_file_load (file, NULL, NULL);
 
1328
 
 
1329
          /* need to re-register the monitor handle for the new uri */
 
1330
          watch_count = THUNAR_FILE_GET_WATCH_COUNT (file);
 
1331
          if (G_LIKELY (watch_count > 0))
 
1332
            {
 
1333
              /* drop the watch_count temporary */
 
1334
              THUNAR_FILE_SET_WATCH_COUNT (file, 1);
 
1335
 
 
1336
              /* drop the previous handle (with the old path) */
 
1337
              thunar_file_unwatch (file);
 
1338
 
 
1339
              /* register the new handle (with the new path) */
 
1340
              thunar_file_watch (file);
 
1341
 
 
1342
              /* reset the watch count */
 
1343
              THUNAR_FILE_SET_WATCH_COUNT (file, watch_count);
 
1344
            }
 
1345
 
 
1346
          G_LOCK (file_cache_mutex);
 
1347
 
 
1348
          /* drop the previous entry from the cache */
 
1349
          g_hash_table_remove (file_cache, previous_file);
 
1350
 
 
1351
          /* drop the reference on the previous file */
 
1352
          g_object_unref (previous_file);
 
1353
 
 
1354
          /* insert the new entry */
 
1355
          g_hash_table_insert (file_cache, g_object_ref (file->gfile), file);
 
1356
 
 
1357
          G_UNLOCK (file_cache_mutex);
 
1358
 
 
1359
          if (!called_from_job)
 
1360
            {
 
1361
              /* tell the associated folder that the file was renamed */
 
1362
              thunarx_file_info_renamed (THUNARX_FILE_INFO (file));
 
1363
 
 
1364
              /* emit the file changed signal */
 
1365
              thunar_file_changed (file);
 
1366
            }
 
1367
 
 
1368
          return TRUE;
 
1369
        }
 
1370
      else
 
1371
        {
 
1372
          g_object_unref (previous_file);
 
1373
 
 
1374
          return FALSE;
 
1375
        }
1326
1376
    }
1327
1377
}
1328
1378