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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpdata.c
* Copyright (C) 2001 Michael Natterer <mitch@gimp.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libgimpbase/gimpbase.h"
#include "core-types.h"
#include "gimp-memsize.h"
#include "gimpdata.h"
#include "gimpidtable.h"
#include "gimpimage.h"
#include "gimptag.h"
#include "gimptagged.h"
#include "gimp-intl.h"
enum
{
DIRTY,
LAST_SIGNAL
};
enum
{
PROP_0,
PROP_ID,
PROP_FILE,
PROP_IMAGE,
PROP_WRITABLE,
PROP_DELETABLE,
PROP_MIME_TYPE
};
struct _GimpDataPrivate
{
gint ID;
GFile *file;
GimpImage *image;
GQuark mime_type;
guint writable : 1;
guint deletable : 1;
guint dirty : 1;
guint internal : 1;
gint freeze_count;
gint64 mtime;
/* Identifies the collection this GimpData belongs to.
* Used when there is not a filename associated with the object.
*/
gchar *collection;
GList *tags;
};
#define GIMP_DATA_GET_PRIVATE(obj) (((GimpData *) (obj))->priv)
static void gimp_data_tagged_iface_init (GimpTaggedInterface *iface);
static void gimp_data_constructed (GObject *object);
static void gimp_data_finalize (GObject *object);
static void gimp_data_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gimp_data_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void gimp_data_name_changed (GimpObject *object);
static gint64 gimp_data_get_memsize (GimpObject *object,
gint64 *gui_size);
static gboolean gimp_data_is_name_editable (GimpViewable *viewable);
static void gimp_data_real_dirty (GimpData *data);
static GimpData * gimp_data_real_duplicate (GimpData *data);
static gint gimp_data_real_compare (GimpData *data1,
GimpData *data2);
static gboolean gimp_data_add_tag (GimpTagged *tagged,
GimpTag *tag);
static gboolean gimp_data_remove_tag (GimpTagged *tagged,
GimpTag *tag);
static GList * gimp_data_get_tags (GimpTagged *tagged);
static gchar * gimp_data_get_identifier (GimpTagged *tagged);
static gchar * gimp_data_get_checksum (GimpTagged *tagged);
static gchar * gimp_data_get_collection (GimpData *data);
G_DEFINE_TYPE_WITH_CODE (GimpData, gimp_data, GIMP_TYPE_RESOURCE,
G_ADD_PRIVATE (GimpData)
G_IMPLEMENT_INTERFACE (GIMP_TYPE_TAGGED,
gimp_data_tagged_iface_init))
#define parent_class gimp_data_parent_class
static guint data_signals[LAST_SIGNAL] = { 0 };
static GimpIdTable *data_id_table = NULL;
static void
gimp_data_class_init (GimpDataClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
GimpViewableClass *viewable_class = GIMP_VIEWABLE_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
data_signals[DIRTY] =
g_signal_new ("dirty",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GimpDataClass, dirty),
NULL, NULL, NULL,
G_TYPE_NONE, 0);
object_class->constructed = gimp_data_constructed;
object_class->finalize = gimp_data_finalize;
object_class->set_property = gimp_data_set_property;
object_class->get_property = gimp_data_get_property;
gimp_object_class->name_changed = gimp_data_name_changed;
gimp_object_class->get_memsize = gimp_data_get_memsize;
viewable_class->name_editable = TRUE;
viewable_class->is_name_editable = gimp_data_is_name_editable;
klass->dirty = gimp_data_real_dirty;
klass->save = NULL;
klass->get_extension = NULL;
klass->copy = NULL;
klass->duplicate = gimp_data_real_duplicate;
klass->compare = gimp_data_real_compare;
g_object_class_install_property (object_class, PROP_ID,
g_param_spec_int ("id", NULL, NULL,
0, G_MAXINT, 0,
GIMP_PARAM_READABLE));
g_object_class_install_property (object_class, PROP_FILE,
g_param_spec_object ("file", NULL, NULL,
G_TYPE_FILE,
GIMP_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_IMAGE,
g_param_spec_object ("image", NULL, NULL,
GIMP_TYPE_IMAGE,
G_PARAM_EXPLICIT_NOTIFY |
GIMP_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_WRITABLE,
g_param_spec_boolean ("writable", NULL, NULL,
FALSE,
GIMP_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_DELETABLE,
g_param_spec_boolean ("deletable", NULL, NULL,
FALSE,
GIMP_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_MIME_TYPE,
g_param_spec_string ("mime-type", NULL, NULL,
NULL,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
data_id_table = gimp_id_table_new ();
}
static void
gimp_data_tagged_iface_init (GimpTaggedInterface *iface)
{
iface->add_tag = gimp_data_add_tag;
iface->remove_tag = gimp_data_remove_tag;
iface->get_tags = gimp_data_get_tags;
iface->get_identifier = gimp_data_get_identifier;
iface->get_checksum = gimp_data_get_checksum;
}
static void
gimp_data_init (GimpData *data)
{
GimpDataPrivate *private = gimp_data_get_instance_private (data);
data->priv = private;
private->ID = gimp_id_table_insert (data_id_table, data);
private->writable = TRUE;
private->deletable = TRUE;
private->dirty = TRUE;
/* freeze the data object during construction */
gimp_data_freeze (data);
}
static void
gimp_data_constructed (GObject *object)
{
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (object);
G_OBJECT_CLASS (parent_class)->constructed (object);
if (! GIMP_DATA_GET_CLASS (object)->save)
private->writable = FALSE;
gimp_data_thaw (GIMP_DATA (object));
}
static void
gimp_data_finalize (GObject *object)
{
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (object);
gimp_id_table_remove (data_id_table, private->ID);
g_clear_object (&private->file);
g_clear_weak_pointer (&private->image);
if (private->tags)
{
g_list_free_full (private->tags, (GDestroyNotify) g_object_unref);
private->tags = NULL;
}
g_clear_pointer (&private->collection, g_free);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_data_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GimpData *data = GIMP_DATA (object);
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (data);
switch (property_id)
{
case PROP_FILE:
gimp_data_set_file (data,
g_value_get_object (value),
private->writable,
private->deletable);
break;
case PROP_IMAGE:
gimp_data_set_image (data,
g_value_get_object (value),
private->writable,
private->deletable);
break;
case PROP_WRITABLE:
private->writable = g_value_get_boolean (value);
break;
case PROP_DELETABLE:
private->deletable = g_value_get_boolean (value);
break;
case PROP_MIME_TYPE:
if (g_value_get_string (value))
private->mime_type = g_quark_from_string (g_value_get_string (value));
else
private->mime_type = 0;
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_data_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (object);
switch (property_id)
{
case PROP_ID:
g_value_set_int (value, private->ID);
break;
case PROP_FILE:
g_value_set_object (value, private->file);
break;
case PROP_IMAGE:
g_value_set_object (value, private->image);
break;
case PROP_WRITABLE:
g_value_set_boolean (value, private->writable);
break;
case PROP_DELETABLE:
g_value_set_boolean (value, private->deletable);
break;
case PROP_MIME_TYPE:
g_value_set_string (value, g_quark_to_string (private->mime_type));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
gimp_data_name_changed (GimpObject *object)
{
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (object);
private->dirty = TRUE;
if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
GIMP_OBJECT_CLASS (parent_class)->name_changed (object);
}
static gint64
gimp_data_get_memsize (GimpObject *object,
gint64 *gui_size)
{
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (object);
gint64 memsize = 0;
memsize += gimp_g_object_get_memsize (G_OBJECT (private->file));
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
gui_size);
}
static gboolean
gimp_data_is_name_editable (GimpViewable *viewable)
{
return gimp_data_is_writable (GIMP_DATA (viewable)) &&
! gimp_data_is_internal (GIMP_DATA (viewable));
}
static void
gimp_data_real_dirty (GimpData *data)
{
gimp_viewable_invalidate_preview (GIMP_VIEWABLE (data));
/* Emit the "name-changed" to signal general dirtiness, our name
* changed implementation will also set the "dirty" flag to TRUE.
*/
gimp_object_name_changed (GIMP_OBJECT (data));
}
static GimpData *
gimp_data_real_duplicate (GimpData *data)
{
if (GIMP_DATA_GET_CLASS (data)->copy)
{
GimpData *new = g_object_new (G_OBJECT_TYPE (data), NULL);
gimp_data_copy (new, data);
return new;
}
return NULL;
}
static gint
gimp_data_real_compare (GimpData *data1,
GimpData *data2)
{
GimpDataPrivate *private1 = GIMP_DATA_GET_PRIVATE (data1);
GimpDataPrivate *private2 = GIMP_DATA_GET_PRIVATE (data2);
/* move the internal objects (like the FG -> BG) gradient) to the top */
if (private1->internal != private2->internal)
return private1->internal ? -1 : 1;
/* keep user-deletable objects above system resource files */
if (private1->deletable != private2->deletable)
return private1->deletable ? -1 : 1;
if (g_strcmp0 (private1->collection, private2->collection) != 0)
return g_strcmp0 (private1->collection, private2->collection);
return gimp_object_name_collate ((GimpObject *) data1,
(GimpObject *) data2);
}
static gboolean
gimp_data_add_tag (GimpTagged *tagged,
GimpTag *tag)
{
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (tagged);
GList *list;
for (list = private->tags; list; list = g_list_next (list))
{
GimpTag *this = GIMP_TAG (list->data);
if (gimp_tag_equals (tag, this))
return FALSE;
}
private->tags = g_list_prepend (private->tags, g_object_ref (tag));
return TRUE;
}
static gboolean
gimp_data_remove_tag (GimpTagged *tagged,
GimpTag *tag)
{
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (tagged);
GList *list;
for (list = private->tags; list; list = g_list_next (list))
{
GimpTag *this = GIMP_TAG (list->data);
if (gimp_tag_equals (tag, this))
{
private->tags = g_list_delete_link (private->tags, list);
g_object_unref (this);
return TRUE;
}
}
return FALSE;
}
static GList *
gimp_data_get_tags (GimpTagged *tagged)
{
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (tagged);
return private->tags;
}
static gchar *
gimp_data_get_identifier (GimpTagged *tagged)
{
GimpData *data = GIMP_DATA (tagged);
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (data);
gchar *identifier = NULL;
gchar *collection = NULL;
g_return_val_if_fail (private->internal || private->file != NULL || private->image != NULL, NULL);
collection = gimp_data_get_collection (data);
/* The identifier is guaranteed to be unique because we use 2 directory
* separators between the collection and the data name. Since the collection
* is either a controlled internal name or built from g_file_get_path(), which
* is guaranteed to be a canonical path, we know it won't contain 2
* separators. Therefore it should be impossible to construct a file path able
* to create duplicate identifiers.
* The last point is obviously that it should not be possible to have
* duplicate data names in a single collection. So every identifier should be
* unique.
*/
identifier = g_strdup_printf ("%s:%s%s%s%s",
private->internal ? "internal" : "external",
collection, G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S,
gimp_object_get_name (GIMP_OBJECT (data)));
g_free (collection);
return identifier;
}
static gchar *
gimp_data_get_checksum (GimpTagged *tagged)
{
return NULL;
}
/*
* A data collection name is either generated from the file path, or set when
* marking a data as internal.
* Several data objects may belong to a same collection. A very common example
* of this in fonts are collections of fonts (e.g. TrueType Collection .TTC
* files).
*/
static gchar *
gimp_data_get_collection (GimpData *data)
{
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (data);
gchar *collection = NULL;
g_return_val_if_fail (private->internal || private->file != NULL || private->image != NULL, NULL);
if (private->file)
{
const gchar *data_dir = gimp_data_directory ();
const gchar *gimp_dir = gimp_directory ();
gchar *path = g_file_get_path (private->file);
gchar *tmp;
if (g_str_has_prefix (path, data_dir))
{
tmp = g_strconcat ("${gimp_data_dir}",
path + strlen (data_dir),
NULL);
collection = g_filename_to_utf8 (tmp, -1, NULL, NULL, NULL);
g_free (tmp);
}
else if (g_str_has_prefix (path, gimp_dir))
{
tmp = g_strconcat ("${gimp_dir}",
path + strlen (gimp_dir),
NULL);
collection = g_filename_to_utf8 (tmp, -1, NULL, NULL, NULL);
g_free (tmp);
}
else if (g_str_has_prefix (path, MYPAINT_BRUSHES_DIR))
{
tmp = g_strconcat ("${mypaint_brushes_dir}",
path + strlen (MYPAINT_BRUSHES_DIR),
NULL);
collection = g_filename_to_utf8 (tmp, -1, NULL, NULL, NULL);
g_free (tmp);
}
else
{
collection = g_filename_to_utf8 (path, -1,
NULL, NULL, NULL);
}
if (! collection)
{
g_printerr ("%s: failed to convert '%s' to utf8.\n",
G_STRFUNC, path);
collection = g_strdup (path);
}
g_free (path);
}
else if (private->image)
{
collection = g_strdup_printf ("[image-id-%d]", gimp_image_get_id (private->image));
}
else if (private->internal)
{
collection = g_strdup (private->collection);
}
return collection;
}
/* public functions */
gint
gimp_data_get_id (GimpData *data)
{
g_return_val_if_fail (GIMP_IS_DATA (data), -1);
return GIMP_DATA_GET_PRIVATE (data)->ID;
}
GimpData *
gimp_data_get_by_id (gint data_id)
{
return (GimpData *) gimp_id_table_lookup (data_id_table, data_id);
}
/**
* gimp_data_save:
* @data: object whose contents are to be saved.
* @error: return location for errors or %NULL
*
* Save the object. If the object is marked as "internal", nothing
* happens. Otherwise, it is saved to disk, using the file name set
* by gimp_data_set_file(). If the save is successful, the object is
* marked as not dirty. If not, an error message is returned using
* the @error argument.
*
* Returns: %TRUE if the object is internal or the save is successful.
**/
gboolean
gimp_data_save (GimpData *data,
GError **error)
{
GimpDataPrivate *private;
gboolean success = FALSE;
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
private = GIMP_DATA_GET_PRIVATE (data);
g_return_val_if_fail (private->writable == TRUE, FALSE);
if (private->internal || private->image != NULL)
{
private->dirty = FALSE;
return TRUE;
}
g_return_val_if_fail (G_IS_FILE (private->file), FALSE);
if (GIMP_DATA_GET_CLASS (data)->save)
{
GOutputStream *output;
output = G_OUTPUT_STREAM (g_file_replace (private->file,
NULL, FALSE, G_FILE_CREATE_NONE,
NULL, error));
if (output)
{
success = GIMP_DATA_GET_CLASS (data)->save (data, output, error);
if (success)
{
if (! g_output_stream_close (output, NULL, error))
{
g_prefix_error (error,
_("Error saving '%s': "),
gimp_file_get_utf8_name (private->file));
success = FALSE;
}
}
else
{
GCancellable *cancellable = g_cancellable_new ();
g_cancellable_cancel (cancellable);
if (error && *error)
{
g_prefix_error (error,
_("Error saving '%s': "),
gimp_file_get_utf8_name (private->file));
}
else
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_WRITE,
_("Error saving '%s'"),
gimp_file_get_utf8_name (private->file));
}
g_output_stream_close (output, cancellable, NULL);
g_object_unref (cancellable);
}
g_object_unref (output);
}
}
if (success)
{
GFileInfo *info = g_file_query_info (private->file,
G_FILE_ATTRIBUTE_TIME_MODIFIED,
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
if (info)
{
private->mtime =
g_file_info_get_attribute_uint64 (info,
G_FILE_ATTRIBUTE_TIME_MODIFIED);
g_object_unref (info);
}
private->dirty = FALSE;
}
return success;
}
/**
* gimp_data_dirty:
* @data: a #GimpData object.
*
* Marks @data as dirty. Unless the object is frozen, this causes
* its preview to be invalidated, and emits a "dirty" signal. If the
* object is frozen, the function has no effect.
**/
void
gimp_data_dirty (GimpData *data)
{
GimpDataPrivate *private;
g_return_if_fail (GIMP_IS_DATA (data));
private = GIMP_DATA_GET_PRIVATE (data);
if (private->freeze_count == 0)
g_signal_emit (data, data_signals[DIRTY], 0);
}
void
gimp_data_clean (GimpData *data)
{
GimpDataPrivate *private;
g_return_if_fail (GIMP_IS_DATA (data));
private = GIMP_DATA_GET_PRIVATE (data);
private->dirty = FALSE;
}
gboolean
gimp_data_is_dirty (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
private = GIMP_DATA_GET_PRIVATE (data);
return private->dirty;
}
/**
* gimp_data_freeze:
* @data: a #GimpData object.
*
* Increments the freeze count for the object. A positive freeze count
* prevents the object from being treated as dirty. Any call to this
* function must be followed eventually by a call to gimp_data_thaw().
**/
void
gimp_data_freeze (GimpData *data)
{
GimpDataPrivate *private;
g_return_if_fail (GIMP_IS_DATA (data));
private = GIMP_DATA_GET_PRIVATE (data);
private->freeze_count++;
}
/**
* gimp_data_thaw:
* @data: a #GimpData object.
*
* Decrements the freeze count for the object. If the freeze count
* drops to zero, the object is marked as dirty, and the "dirty"
* signal is emitted. It is an error to call this function without
* having previously called gimp_data_freeze().
**/
void
gimp_data_thaw (GimpData *data)
{
GimpDataPrivate *private;
g_return_if_fail (GIMP_IS_DATA (data));
private = GIMP_DATA_GET_PRIVATE (data);
g_return_if_fail (private->freeze_count > 0);
private->freeze_count--;
if (private->freeze_count == 0)
gimp_data_dirty (data);
}
gboolean
gimp_data_is_frozen (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
private = GIMP_DATA_GET_PRIVATE (data);
return private->freeze_count > 0;
}
/**
* gimp_data_delete_from_disk:
* @data: a #GimpData object.
* @error: return location for errors or %NULL
*
* Deletes the object from disk. If the object is marked as "internal",
* nothing happens. Otherwise, if the file exists whose name has been
* set by gimp_data_set_file(), it is deleted. Obviously this is
* a potentially dangerous function, which should be used with care.
*
* Returns: %TRUE if the object is internal to Gimp, or the deletion is
* successful.
**/
gboolean
gimp_data_delete_from_disk (GimpData *data,
GError **error)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
private = GIMP_DATA_GET_PRIVATE (data);
g_return_val_if_fail (private->file != NULL, FALSE);
g_return_val_if_fail (private->deletable == TRUE, FALSE);
if (private->internal)
return TRUE;
return g_file_delete (private->file, NULL, error);
}
const gchar *
gimp_data_get_extension (GimpData *data)
{
g_return_val_if_fail (GIMP_IS_DATA (data), NULL);
if (GIMP_DATA_GET_CLASS (data)->get_extension)
return GIMP_DATA_GET_CLASS (data)->get_extension (data);
return NULL;
}
/**
* gimp_data_set_file:
* @data: A #GimpData object
* @file: File to assign to @data.
* @writable: %TRUE if we want to be able to write to this file.
* @deletable: %TRUE if we want to be able to delete this file.
*
* This function assigns a file to @data, and sets some flags
* according to the properties of the file. If @writable is %TRUE,
* and the user has permission to write or overwrite the requested
* file name, and a "save" method exists for @data's object type, then
* @data is marked as writable.
**/
void
gimp_data_set_file (GimpData *data,
GFile *file,
gboolean writable,
gboolean deletable)
{
GimpDataPrivate *private;
gchar *path;
g_return_if_fail (GIMP_IS_DATA (data));
g_return_if_fail (G_IS_FILE (file));
path = g_file_get_path (file);
g_return_if_fail (path != NULL);
g_return_if_fail (g_path_is_absolute (path));
g_free (path);
private = GIMP_DATA_GET_PRIVATE (data);
if (private->internal)
return;
g_return_if_fail (private->image == NULL);
g_set_object (&private->file, file);
private->writable = FALSE;
private->deletable = FALSE;
/* if the data is supposed to be writable or deletable,
* still check if it really is
*/
if (writable || deletable)
{
GFileInfo *info;
if (g_file_query_exists (private->file, NULL)) /* check if it exists */
{
info = g_file_query_info (private->file,
G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
/* and we can write it */
if (info)
{
if (g_file_info_get_attribute_boolean (info,
G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
{
private->writable = writable ? TRUE : FALSE;
private->deletable = deletable ? TRUE : FALSE;
}
g_object_unref (info);
}
}
else /* OR it doesn't exist */
{
GFile *parent = g_file_get_parent (private->file);
info = g_file_query_info (parent,
G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
G_FILE_QUERY_INFO_NONE,
NULL, NULL);
/* and we can write to its parent directory */
if (info)
{
if (g_file_info_get_attribute_boolean (info,
G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
{
private->writable = writable ? TRUE : FALSE;
private->deletable = deletable ? TRUE : FALSE;
}
g_object_unref (info);
}
g_object_unref (parent);
}
/* if we can't save, we are not writable */
if (! GIMP_DATA_GET_CLASS (data)->save)
private->writable = FALSE;
}
}
GFile *
gimp_data_get_file (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), NULL);
private = GIMP_DATA_GET_PRIVATE (data);
return private->file;
}
/**
* gimp_data_set_image:
* @data: A #GimpData object
* @image: Image to assign to @data.
* @writable: %TRUE if we want to be able to write to this file.
* @deletable: %TRUE if we want to be able to delete this file.
*
* This function assigns an image to @data. This can only be done if no file has
* been assigned (a non-internal data can be attached either to a file or to an
* image).
**/
void
gimp_data_set_image (GimpData *data,
GimpImage *image,
gboolean writable,
gboolean deletable)
{
GimpDataPrivate *private;
g_return_if_fail (GIMP_IS_DATA (data));
g_return_if_fail (GIMP_IS_IMAGE (image));
private = GIMP_DATA_GET_PRIVATE (data);
if (private->internal)
return;
g_return_if_fail (private->file == NULL);
g_set_weak_pointer (&private->image, image);
private->writable = writable ? TRUE : FALSE;
private->deletable = deletable ? TRUE : FALSE;
g_object_notify (G_OBJECT (data), "image");
}
GimpImage *
gimp_data_get_image (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), NULL);
private = GIMP_DATA_GET_PRIVATE (data);
return private->image;
}
/**
* gimp_data_create_filename:
* @data: a #Gimpdata object.
* @dest_dir: directory in which to create a file name.
*
* This function creates a unique file name to be used for saving
* a representation of @data in the directory @dest_dir. If the
* user does not have write permission in @dest_dir, then @data
* is marked as "not writable", so you should check on this before
* assuming that @data can be saved.
**/
void
gimp_data_create_filename (GimpData *data,
GFile *dest_dir)
{
GimpDataPrivate *private;
gchar *safename;
gchar *basename;
GFile *file;
gint i;
gint unum = 1;
GError *error = NULL;
g_return_if_fail (GIMP_IS_DATA (data));
g_return_if_fail (G_IS_FILE (dest_dir));
private = GIMP_DATA_GET_PRIVATE (data);
if (private->internal)
return;
safename = g_strstrip (g_strdup (gimp_object_get_name (data)));
if (safename[0] == '.')
safename[0] = '-';
for (i = 0; safename[i]; i++)
if (strchr ("\\/*?\"`'<>{}|\n\t ;:$^&", safename[i]))
safename[i] = '-';
basename = g_strconcat (safename, gimp_data_get_extension (data), NULL);
file = g_file_get_child_for_display_name (dest_dir, basename, &error);
g_free (basename);
if (! file)
{
g_warning ("gimp_data_create_filename:\n"
"g_file_get_child_for_display_name() failed for '%s': %s",
gimp_object_get_name (data), error->message);
g_clear_error (&error);
g_free (safename);
return;
}
while (g_file_query_exists (file, NULL))
{
g_object_unref (file);
basename = g_strdup_printf ("%s-%d%s",
safename,
unum++,
gimp_data_get_extension (data));
file = g_file_get_child_for_display_name (dest_dir, basename, NULL);
g_free (basename);
}
g_free (safename);
gimp_data_set_file (data, file, TRUE, TRUE);
g_object_unref (file);
}
static const gchar *tag_blacklist[] = { "brushes",
"dynamics",
"patterns",
"palettes",
"gradients",
"tool-presets" };
/**
* gimp_data_set_folder_tags:
* @data: a #Gimpdata object.
* @top_directory: the top directory of the currently processed data
* hierarchy.
*
* Sets tags based on all folder names below top_directory. So if the
* data's filename is e.g.
* /home/foo/.config/GIMP/X.Y/brushes/Flowers/Roses/rose.gbr, it will
* add "Flowers" and "Roses" tags.
*
* if the top directory (as passed, or as derived from the data's
* filename) does not end with one of the default data directory names
* (brushes, patterns etc), its name will be added as tag too.
**/
void
gimp_data_set_folder_tags (GimpData *data,
GFile *top_directory)
{
GimpDataPrivate *private;
gchar *tmp;
gchar *dirname;
gchar *top_path;
g_return_if_fail (GIMP_IS_DATA (data));
g_return_if_fail (G_IS_FILE (top_directory));
private = GIMP_DATA_GET_PRIVATE (data);
if (private->internal)
return;
g_return_if_fail (private->file != NULL);
tmp = g_file_get_path (private->file);
dirname = g_path_get_dirname (tmp);
g_free (tmp);
top_path = g_file_get_path (top_directory);
g_return_if_fail (g_str_has_prefix (dirname, top_path));
/* walk up the hierarchy and set each folder on the way as tag,
* except the top_directory
*/
while (strcmp (dirname, top_path))
{
gchar *basename = g_path_get_basename (dirname);
GimpTag *tag = gimp_tag_new (basename);
gimp_tag_set_internal (tag, TRUE);
gimp_tagged_add_tag (GIMP_TAGGED (data), tag);
g_object_unref (tag);
g_free (basename);
tmp = g_path_get_dirname (dirname);
g_free (dirname);
dirname = tmp;
}
g_free (top_path);
if (dirname)
{
gchar *basename = g_path_get_basename (dirname);
gint i;
for (i = 0; i < G_N_ELEMENTS (tag_blacklist); i++)
{
if (! strcmp (basename, tag_blacklist[i]))
break;
}
if (i == G_N_ELEMENTS (tag_blacklist))
{
GimpTag *tag = gimp_tag_new (basename);
gimp_tag_set_internal (tag, TRUE);
gimp_tagged_add_tag (GIMP_TAGGED (data), tag);
g_object_unref (tag);
}
g_free (basename);
g_free (dirname);
}
}
const gchar *
gimp_data_get_mime_type (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), NULL);
private = GIMP_DATA_GET_PRIVATE (data);
return g_quark_to_string (private->mime_type);
}
gboolean
gimp_data_is_writable (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
private = GIMP_DATA_GET_PRIVATE (data);
return private->writable;
}
gboolean
gimp_data_is_deletable (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
private = GIMP_DATA_GET_PRIVATE (data);
return private->deletable;
}
void
gimp_data_set_mtime (GimpData *data,
gint64 mtime)
{
GimpDataPrivate *private;
g_return_if_fail (GIMP_IS_DATA (data));
private = GIMP_DATA_GET_PRIVATE (data);
private->mtime = mtime;
}
gint64
gimp_data_get_mtime (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), 0);
private = GIMP_DATA_GET_PRIVATE (data);
return private->mtime;
}
gboolean
gimp_data_is_copyable (GimpData *data)
{
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
return GIMP_DATA_GET_CLASS (data)->copy != NULL;
}
/**
* gimp_data_copy:
* @data: a #GimpData object
* @src_data: the #GimpData object to copy from
*
* Copies @src_data to @data. Only the object data is copied: the
* name, file name, preview, etc. are not copied.
**/
void
gimp_data_copy (GimpData *data,
GimpData *src_data)
{
g_return_if_fail (GIMP_IS_DATA (data));
g_return_if_fail (GIMP_IS_DATA (src_data));
g_return_if_fail (GIMP_DATA_GET_CLASS (data)->copy != NULL);
g_return_if_fail (GIMP_DATA_GET_CLASS (data)->copy ==
GIMP_DATA_GET_CLASS (src_data)->copy);
if (data != src_data)
GIMP_DATA_GET_CLASS (data)->copy (data, src_data);
}
gboolean
gimp_data_is_duplicatable (GimpData *data)
{
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
if (GIMP_DATA_GET_CLASS (data)->duplicate == gimp_data_real_duplicate)
return gimp_data_is_copyable (data);
else
return GIMP_DATA_GET_CLASS (data)->duplicate != NULL;
}
/**
* gimp_data_duplicate:
* @data: a #GimpData object
*
* Creates a copy of @data, if possible. Only the object data is
* copied: the newly created object is not automatically given an
* object name, file name, preview, etc.
*
* Returns: (nullable) (transfer full): the newly created copy, or %NULL if
* @data cannot be copied.
**/
GimpData *
gimp_data_duplicate (GimpData *data)
{
g_return_val_if_fail (GIMP_IS_DATA (data), NULL);
if (gimp_data_is_duplicatable (data))
{
GimpData *new = GIMP_DATA_GET_CLASS (data)->duplicate (data);
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (new);
g_object_set (new,
"name", NULL,
"writable", GIMP_DATA_GET_CLASS (new)->save != NULL,
"deletable", TRUE,
NULL);
g_clear_object (&private->file);
return new;
}
return NULL;
}
/**
* gimp_data_make_internal:
* @data: a #GimpData object.
* @collection: internal collection title @data belongs to.
*
* Mark @data as "internal" to Gimp, which means that it will not be
* saved to disk. Note that if you do this, later calls to
* gimp_data_save() and gimp_data_delete_from_disk() will
* automatically return successfully without giving any warning.
*
* The @collection shall be an untranslated globally unique string
* that identifies the internal object collection across sessions.
**/
void
gimp_data_make_internal (GimpData *data,
const gchar *collection)
{
GimpDataPrivate *private;
g_return_if_fail (GIMP_IS_DATA (data));
private = GIMP_DATA_GET_PRIVATE (data);
g_clear_object (&private->file);
g_free (private->collection);
private->collection = g_strdup (collection);
private->writable = FALSE;
private->deletable = FALSE;
private->internal = TRUE;
}
gboolean
gimp_data_is_internal (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
private = GIMP_DATA_GET_PRIVATE (data);
return private->internal;
}
/**
* gimp_data_compare:
* @data1: a #GimpData object.
* @data2: another #GimpData object.
*
* Compares two data objects for use in sorting. Objects marked as
* "internal" come first, then user-writable objects, then system data
* files. In these three groups, the objects are sorted alphabetically
* by name, using gimp_object_name_collate().
*
* Returns: -1 if @data1 compares before @data2,
* 0 if they compare equal,
* 1 if @data1 compares after @data2.
**/
gint
gimp_data_compare (GimpData *data1,
GimpData *data2)
{
g_return_val_if_fail (GIMP_IS_DATA (data1), 0);
g_return_val_if_fail (GIMP_IS_DATA (data2), 0);
g_return_val_if_fail (GIMP_DATA_GET_CLASS (data1)->compare ==
GIMP_DATA_GET_CLASS (data2)->compare, 0);
return GIMP_DATA_GET_CLASS (data1)->compare (data1, data2);
}
/**
* gimp_data_identify:
* @data: a #GimpData object.
* @name: name of the #GimpData object.
* @collection: text uniquely identifying the collection @data belongs to.
* @is_internal: whether this is internal data.
*
* Determine whether (@name, @collection, @is_internal) uniquely identify @data.
*
* Returns: %TRUE if the triplet identifies @data, %FALSE otherwise.
**/
gboolean
gimp_data_identify (GimpData *data,
const gchar *name,
const gchar *collection,
gboolean is_internal)
{
gchar *current_collection = gimp_data_get_collection (data);
gboolean identified;
identified = (is_internal == gimp_data_is_internal (data) &&
g_strcmp0 (collection, current_collection) == 0 &&
/* Internal data have unique collection names. Moreover
* their names can be localized so it should not be
* relied upon for comparison.
*/
(is_internal ? TRUE : g_strcmp0 (name, gimp_object_get_name (GIMP_OBJECT (data))) == 0));
g_free (current_collection);
return identified;
}
/**
* gimp_data_get_identifiers:
* @data: a #GimpData object.
* @name: name of the #GimpData object.
* @collection: text uniquely identifying the collection @data belongs to.
* @is_internal: whether this is internal data.
*
* Generates a triplet of identifiers which, together, should uniquely identify
* this @data.
* @name will be the same value as gimp_object_get_name() and @is_internal the
* same value as returned by gimp_data_is_internal(), except that it is not
* enough because two data from different sources may end up having the same
* name. Nevertheless all data names within a single collection of data are
* unique. @collection therefore identifies the source collection. And these 3
* identifiers together are enough to identify a GimpData.
*
* Internally the collection will likely be a single file name, therefore
* @collection will be constructed from the file name (if it exists, or an
* opaque identifier string otherwise, for internal data). Nevertheless you
* should not take this for granted and should always use this string as an
* opaque identifier only to be reused in gimp_data_identify().
**/
void
gimp_data_get_identifiers (GimpData *data,
gchar **name,
gchar **collection,
gboolean *is_internal)
{
*collection = gimp_data_get_collection (data);
*name = g_strdup (gimp_object_get_name (GIMP_OBJECT (data)));
*is_internal = gimp_data_is_internal (data);
}
/**
* gimp_data_error_quark:
*
* This function is used to implement the GIMP_DATA_ERROR macro. It
* shouldn't be called directly.
*
* Returns: the #GQuark to identify error in the GimpData error domain.
**/
GQuark
gimp_data_error_quark (void)
{
return g_quark_from_static_string ("gimp-data-error-quark");
}
|