2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2007 Match Grun and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Contains address clipboard objects and related functions. The address
22 * clipboard is implemented as a linked list of AddrSelectItem objects.
23 * The address clipboard offers two groups of functions:
25 * a) Cut, copy and paste of address item objects (ItemFolder, ItemGroup,
26 * ItemPerson) into a folder. With this method, we can paste ItemPerson
27 * objects but not unattached ItemEMail objects into a folder. ItemEMail
28 * objects are owned by an ItemPerson object. Any ItemEMail objects that
29 * appear in the clipboard are ignored. If an ItemPerson object is found,
30 * the ItemPerson *and* ItemEMail objects that it owns are pasted.
32 * b) Copy and paste of ItemEMail address objects only into (below)
33 * ItemPerson objects. All ItemEMail objects which are owned by
34 * ItemPerson and referenced by ItemGroup objects are pasted. Any
35 * ItemFolder objects in the clipboard, and any objects owned by
36 * ItemFolder objects are ignored.
38 * Objects are inserted to the clipboard by copying (cloning)
39 * AddrSelectItem objects from the address books selection list to the
40 * clipboard's internal selection list. The clipboard makes use of the
41 * object id's and address cache id's to access objects contained in
42 * the address cache. If the referenced object is not found, it is
43 * ignored. This eliminates the need to delete pointers in multiple
44 * linked lists when an address object is deleted.
50 #include <glib/gi18n.h>
52 #include "addrcache.h"
54 #include "addrselect.h"
55 #include "addrindex.h"
57 #include "alertpanel.h"
63 AddressClipboard *addrclip_create( void ) {
64 AddressClipboard *clipBoard;
66 clipBoard = g_new0( AddressClipboard, 1 );
67 clipBoard->cutFlag = FALSE;
68 clipBoard->objectList = NULL;
75 void addrclip_clear( AddressClipboard *clipBoard ) {
79 g_return_if_fail( clipBoard != NULL );
80 node = clipBoard->objectList;
83 addrselect_item_free( item );
85 node = g_list_next( node );
87 g_list_free( clipBoard->objectList );
88 clipBoard->objectList = NULL;
92 * Free up a clipboard.
94 void addrclip_free( AddressClipboard *clipBoard ) {
95 g_return_if_fail( clipBoard != NULL );
97 addrclip_clear( clipBoard );
98 clipBoard->cutFlag = FALSE;
102 * Setup reference to address index.
104 void addrclip_set_index(
105 AddressClipboard *clipBoard, AddressIndex *addrIndex )
107 g_return_if_fail( clipBoard != NULL );
108 g_return_if_fail( addrIndex != NULL );
109 clipBoard->addressIndex = addrIndex;
113 * Test whether clipboard is empty.
114 * Enter: clipBoard Clipboard.
115 * Return: TRUE if clipboard is empty.
117 gboolean addrclip_is_empty( AddressClipboard *clipBoard ) {
118 gboolean retVal = TRUE;
121 if( clipBoard->objectList ) retVal = FALSE;
127 * Add a list of address selection objects to clipbard.
128 * Enter: clipBoard Clipboard.
129 * addrList List of address selection objects.
131 void addrclip_add( AddressClipboard *clipBoard, AddrSelectList *asl ) {
134 g_return_if_fail( clipBoard != NULL );
135 g_return_if_fail( asl != NULL );
136 node = asl->listSelect;
138 AddrSelectItem *item, *itemCopy;
141 itemCopy = addrselect_item_copy( item );
142 clipBoard->objectList =
143 g_list_append( clipBoard->objectList, itemCopy );
144 node = g_list_next( node );
149 * Show clipboard contents.
150 * Enter: clipBoard Clipboard.
151 * stream Output stream.
153 void addrclip_list_show( AddressClipboard *clipBoard, FILE *stream ) {
158 g_return_if_fail( clipBoard != NULL );
159 node = clipBoard->objectList;
160 while( node != NULL ) {
161 AddrSelectItem *item;
164 addrselect_item_print( item, stream );
166 cache = addrindex_get_cache( clipBoard->addressIndex, item->cacheID );
167 aio = addrcache_get_object( cache, item->uid );
169 if( ADDRITEM_TYPE(aio) == ITEMTYPE_PERSON ) {
170 addritem_print_item_person( ( ItemPerson * ) aio, stream );
172 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_EMAIL ) {
173 addritem_print_item_email( ( ItemEMail * ) aio, stream );
175 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
176 addritem_print_item_group( ( ItemGroup * ) aio, stream );
178 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
179 addritem_print_item_folder( ( ItemFolder * ) aio, stream );
182 node = g_list_next( node );
186 /* Pasted address pointers */
187 typedef struct _AddrClip_EMail_ AddrClip_EMail;
188 struct _AddrClip_EMail_ {
194 * Free up specified list of addresses.
196 static void addrclip_free_copy_list( GList *copyList ) {
201 AddrClip_EMail *em = node->data;
206 node = g_list_next( node );
211 * Paste person into cache.
212 * Enter: cache Address cache to paste into.
213 * folder Folder to store
214 * person Person to paste.
215 * copyLIst List of email addresses pasted.
216 * Return: Update list of email addresses pasted.
218 static GList *addrclip_cache_add_person(
219 AddressCache *cache, ItemFolder *folder, ItemPerson *person,
222 ItemPerson *newPerson;
225 UserAttribute *attrib;
226 UserAttribute *newAttrib;
231 newPerson = addritem_copy_item_person( person );
232 addrcache_id_person( cache, newPerson );
233 addrcache_folder_add_person( cache, folder, newPerson );
235 /* Copy email addresses */
236 node = person->listEMail;
239 newEMail = addritem_copy_item_email( email );
240 addrcache_id_email( cache, newEMail );
241 addrcache_person_add_email( cache, newPerson, newEMail );
242 node = g_list_next( node );
244 /* Take a copy of the original */
245 em = g_new0( AddrClip_EMail, 1 );
246 em->original = email;
248 copyList = g_list_append( copyList, em );
251 /* Copy user attributes */
252 node = person->listAttrib;
255 newAttrib = addritem_copy_attribute( attrib );
256 addrcache_id_attribute( cache, newAttrib );
257 addritem_person_add_attribute( newPerson, newAttrib );
258 node = g_list_next( node );
261 /* Set picture name and create picture file (from copy) if missing */
262 addritem_person_set_picture(newPerson, ADDRITEM_ID(newPerson));
263 if( strcmp(ADDRITEM_ID(newPerson), ADDRITEM_ID(person)) ) {
265 gchar *newPictureFile;
267 pictureFile = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S,
268 person->picture, ".png", NULL );
269 newPictureFile = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S,
270 newPerson->picture, ".png", NULL );
271 if (file_exist(pictureFile, FALSE) && !file_exist(newPictureFile, FALSE)) {
272 debug_print("copying contact picture file: %s -> %s\n", person->picture, newPerson->picture);
273 copy_file(pictureFile, newPictureFile, FALSE);
275 g_free( pictureFile );
276 g_free( newPictureFile );
283 * Search for new email record in copied email list.
284 * Enter: copyList List of copied email address mappings.
285 * emailOrig Original email item.
286 * Return: New email item corresponding to original item if pasted. Or NULL if
289 static ItemEMail *addrclip_find_copied_email(
290 GList *copyList, ItemEMail *emailOrig )
292 ItemEMail *emailCopy;
300 if( em->original == emailOrig ) {
301 emailCopy = em->copy;
304 node = g_list_next( node );
310 * Paste group into cache.
311 * Enter: cache Address cache to paste into.
312 * folder Folder to store
313 * group Group to paste.
314 * copyList List of email addresses pasted.
315 * Return: Group added.
317 static ItemGroup *addrclip_cache_add_group(
318 AddressCache *cache, ItemFolder *folder, ItemGroup *group,
322 ItemEMail *emailOrig, *emailCopy;
326 newGroup = addritem_copy_item_group( group );
327 addrcache_id_group( cache, newGroup );
328 addrcache_folder_add_group( cache, folder, newGroup );
330 /* Add references of copied addresses to group */
331 node = group->listEMail;
333 emailOrig = ( ItemEMail * ) node->data;
334 emailCopy = addrclip_find_copied_email( copyList, emailOrig );
336 addrcache_group_add_email( cache, newGroup, emailCopy );
338 node = g_list_next( node );
344 * Copy specified folder into cache. Note this functions uses pointers to
345 * folders to copy from. There should not be any deleted items referenced
346 * by these pointers!!!
347 * Enter: cache Address cache to copy into.
348 * targetFolder Target folder.
349 * folder Folder to copy.
350 * Return: Folder added.
352 static ItemFolder *addrclip_cache_copy_folder(
353 AddressCache *cache, ItemFolder *targetFolder, ItemFolder *folder )
355 ItemFolder *newFolder;
361 newFolder = addritem_copy_item_folder( folder );
362 addrcache_id_folder( cache, newFolder );
363 addrcache_folder_add_folder( cache, targetFolder, newFolder );
365 /* Copy people to new folder */
367 node = folder->listPerson;
369 ItemPerson *item = node->data;
370 node = g_list_next( node );
371 copyList = addrclip_cache_add_person(
372 cache, newFolder, item, copyList );
375 /* Copy groups to new folder */
376 node = folder->listGroup;
378 ItemGroup *item = node->data;
379 node = g_list_next( node );
380 newGroup = addrclip_cache_add_group(
381 cache, newFolder, item, copyList );
383 g_list_free( copyList );
385 /* Copy folders to new folder (recursive) */
386 node = folder->listFolder;
388 ItemFolder *item = node->data;
389 node = g_list_next( node );
390 addrclip_cache_copy_folder( cache, newFolder, item );
396 static gboolean addrclip_is_subfolder_of(ItemFolder *is_parent, ItemFolder *is_child)
401 g_return_val_if_fail(is_parent != NULL, FALSE);
402 g_return_val_if_fail(is_child != NULL, FALSE);
404 if (is_parent == is_child)
408 obj = folder->obj.parent;
410 if ((void*)obj == (void*)is_parent)
418 * Paste item list into address book.
419 * Enter: cache Target address cache.
420 * folder Target folder where data is pasted.
421 * itemList List of items to paste.
422 * clipBoard Clipboard.
423 * Return: List of group or folder items added.
425 static GList *addrclip_cache_add_folder(
426 AddressCache *cache, ItemFolder *folder, GList *itemList,
427 AddressClipboard *clipBoard )
431 AddrSelectItem *item;
433 AddressCache *cacheFrom;
443 node = g_list_next( node );
445 cacheFrom = addrindex_get_cache(
446 clipBoard->addressIndex, item->cacheID );
447 if( cacheFrom == NULL ) continue;
449 aio = addrcache_get_object( cacheFrom, item->uid );
451 if( ADDRITEM_TYPE(aio) == ITEMTYPE_PERSON ) {
454 person = ( ItemPerson * ) aio;
455 copyList = addrclip_cache_add_person(
456 cache, folder, person, copyList );
459 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_EMAIL ) {
462 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
463 haveGroups = TRUE; /* Process later */
465 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
466 ItemFolder *itemFolder, *newFolder;
468 itemFolder = ( ItemFolder * ) aio;
469 if (!addrclip_is_subfolder_of(itemFolder, folder)) {
470 newFolder = addrclip_cache_copy_folder(
471 cache, folder, itemFolder );
473 g_list_append( folderGroup, newFolder );
476 _("Cannot copy a folder to itself or to its sub-structure.") );
482 if( item->objectType == ITEMTYPE_DATASOURCE ) {
484 * Must be an address book - allow copy only if
485 * copying from a different cache.
487 if( cache != cacheFrom ) {
488 ItemFolder *itemFolder, *newFolder;
490 itemFolder = cacheFrom->rootFolder;
491 newFolder = addrclip_cache_copy_folder(
492 cache, folder, itemFolder );
493 addritem_folder_set_name( newFolder,
494 addrcache_get_name( cacheFrom ) );
496 g_list_append( folderGroup, newFolder );
499 _("Cannot copy an address book to itself.") );
505 /* Finally add any groups */
510 node = g_list_next( node );
511 cacheFrom = addrindex_get_cache(
512 clipBoard->addressIndex, item->cacheID );
513 if( cacheFrom == NULL ) continue;
514 aio = addrcache_get_object( cacheFrom, item->uid );
516 if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
517 ItemGroup *group, *newGroup;
519 group = ( ItemGroup * ) aio;
520 newGroup = addrclip_cache_add_group(
521 cache, folder, group, copyList );
523 g_list_append( folderGroup, newGroup );
530 addrclip_free_copy_list( copyList );
531 g_list_free( copyList );
538 * Move items in list into new folder
539 * Enter: cache Target address cache.
540 * targetFolder Target folder where data is pasted.
541 * itemList List of items to paste.
542 * clipBoard Clipboard.
543 * Return: List of group or folder items added.
545 static GList *addrclip_cache_move_items(
546 AddressCache *cache, ItemFolder *targetFolder, GList *itemList,
547 AddressClipboard *clipBoard )
551 AddrSelectItem *item;
553 AddressCache *cacheFrom;
559 node = g_list_next( node );
560 cacheFrom = addrindex_get_cache(
561 clipBoard->addressIndex, item->cacheID );
562 if( cacheFrom == NULL ) continue;
563 aio = addrcache_get_object( cacheFrom, item->uid );
565 if( ADDRITEM_TYPE(aio) == ITEMTYPE_PERSON ) {
568 person = ( ItemPerson * ) aio;
569 addrcache_folder_move_person(
570 cache, person, targetFolder );
572 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
575 group = ( ItemGroup * ) aio;
576 addrcache_folder_move_group(
577 cache, group, targetFolder );
578 folderGroup = g_list_append( folderGroup, group );
580 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
581 ItemFolder *folder = ( ItemFolder * ) aio;
583 if (!addrclip_is_subfolder_of(folder, targetFolder)) {
584 addrcache_folder_move_folder(
585 cache, folder, targetFolder );
587 g_list_append( folderGroup, folder );
590 _("Cannot move a folder to itself or to its sub-structure.") );
599 * Get address cache of first item in list. This assumes that all items in
600 * the clipboard are located in the same cache.
601 * Enter: clipBoard Clipboard.
602 * Return: List of group or folder items added.
604 static AddressCache *addrclip_list_get_cache( AddressClipboard *clipBoard ) {
607 AddrSelectItem *item;
610 itemList = clipBoard->objectList;
612 item = itemList->data;
613 cache = addrindex_get_cache(
614 clipBoard->addressIndex, item->cacheID );
620 * Paste (copy) clipboard into address book.
621 * Enter: clipBoard Clipboard.
622 * book Target address book.
623 * folder Target folder where data is pasted, or null for root folder.
624 * Return: List of group or folder items added.
626 GList *addrclip_paste_copy(
627 AddressClipboard *clipBoard, AddressBookFile *book,
634 g_return_val_if_fail( clipBoard != NULL, NULL );
636 cache = book->addressCache;
637 if( folder == NULL ) folder = cache->rootFolder;
640 itemList = clipBoard->objectList;
641 folderGroup = addrclip_cache_add_folder(
642 cache, folder, itemList, clipBoard );
648 * Remove items that were cut from clipboard.
649 * Enter: clipBoard Clipboard.
651 void addrclip_delete_item( AddressClipboard *clipBoard ) {
652 AddrSelectItem *item;
654 AddressCache *cacheFrom;
657 /* If cutting within current cache, no deletion is necessary */
658 if( clipBoard->moveFlag ) return;
661 node = clipBoard->objectList;
664 node = g_list_next( node );
665 cacheFrom = addrindex_get_cache(
666 clipBoard->addressIndex, item->cacheID );
667 if( cacheFrom == NULL ) continue;
668 aio = addrcache_get_object( cacheFrom, item->uid );
670 if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
673 group = ( ItemGroup * ) aio;
674 group = addrcache_remove_group( cacheFrom, group );
676 addritem_free_item_group( group );
682 /* Remove persons and folders */
683 node = clipBoard->objectList;
686 node = g_list_next( node );
688 cacheFrom = addrindex_get_cache(
689 clipBoard->addressIndex, item->cacheID );
690 if( cacheFrom == NULL ) continue;
692 aio = addrcache_get_object( cacheFrom, item->uid );
694 if( ADDRITEM_TYPE(aio) == ITEMTYPE_PERSON ) {
697 person = ( ItemPerson * ) aio;
698 person = addrcache_remove_person( cacheFrom, person );
700 addritem_free_item_person( person );
703 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
704 ItemFolder *itemFolder;
706 itemFolder = ( ItemFolder * ) aio;
707 itemFolder = addrcache_remove_folder_delete(
708 cacheFrom, itemFolder );
709 addritem_free_item_folder( itemFolder );
716 * Paste (move) clipboard into address book.
717 * Enter: clipBoard Clipboard.
718 * book Target address book.
719 * folder Target folder where data is pasted, or null for root folder.
720 * Return: List of group or folder items added.
722 GList *addrclip_paste_cut(
723 AddressClipboard *clipBoard, AddressBookFile *book,
726 AddressCache *cache, *cacheFrom;
730 g_return_val_if_fail( clipBoard != NULL, NULL );
732 cache = book->addressCache;
733 if( folder == NULL ) folder = cache->rootFolder;
736 clipBoard->moveFlag = FALSE;
737 cacheFrom = addrclip_list_get_cache( clipBoard );
738 if( cacheFrom && cacheFrom == cache ) {
739 /* Move items between folders in same book */
740 itemList = clipBoard->objectList;
741 folderGroup = addrclip_cache_move_items(
742 cache, folder, itemList, clipBoard );
743 clipBoard->moveFlag = TRUE;
746 /* Move items across address books */
747 itemList = clipBoard->objectList;
748 folderGroup = addrclip_cache_add_folder(
749 cache, folder, itemList, clipBoard );
756 * ============================================================================
757 * Paste address only.
758 * ============================================================================
762 * Copy email addresses from specified list.
763 * Enter: cache Address cache to paste into.
764 * target Person to receive email addresses.
765 * listEMail List of email addresses.
766 * Return: Number of addresses added.
768 static gint addrclip_person_add_email(
769 AddressCache *cache, ItemPerson *target, GList *listEMail )
774 /* Copy email addresses */
778 ItemEMail *email, *newEMail;
781 newEMail = addritem_copy_item_email( email );
782 addrcache_id_email( cache, newEMail );
783 addrcache_person_add_email( cache, target, newEMail );
784 node = g_list_next( node );
791 * Paste (copy) E-Mail addresses from clipboard into specified person.
792 * Enter: aio Address item to copy from.
793 * cache Target address cache.
794 * person Target person where data is pasted.
795 * Return: Number of EMail records added.
797 static gint addrclip_copy_email_to_person(
798 AddrItemObject *aio, AddressCache *cache, ItemPerson *person )
805 if( ADDRITEM_TYPE(aio) == ITEMTYPE_PERSON ) {
806 ItemPerson *fromPerson;
808 fromPerson = ( ItemPerson * ) aio;
809 listEMail = fromPerson->listEMail;
810 cnt += addrclip_person_add_email(
811 cache, person, listEMail );
813 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_EMAIL ) {
814 ItemEMail *email, *newEMail;
816 email = ( ItemEMail * ) aio;
817 newEMail = addritem_copy_item_email( email );
818 addrcache_id_email( cache, newEMail );
819 addrcache_person_add_email( cache, person, newEMail );
822 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
825 group = ( ItemGroup * ) aio;
826 listEMail = group->listEMail;
827 cnt += addrclip_person_add_email(
828 cache, person, listEMail );
830 else if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
832 AddrItemObject *item;
835 folder = ( ItemFolder * ) aio;
836 node = folder->listPerson;
839 node = g_list_next( node );
840 cnt += addrclip_copy_email_to_person( item, cache, person );
843 node = folder->listGroup;
846 node = g_list_next( node );
847 cnt += addrclip_copy_email_to_person( item, cache, person );
850 node = folder->listFolder;
853 node = g_list_next( node );
854 cnt += addrclip_copy_email_to_person( item, cache, person );