public void HandleMergeTagsCommand (object obj, EventArgs args)
{
Tag[] tags = tag_selection_widget.TagHighlight;
if (tags.Length < 2)
return;
// Translators, The singular case will never happen here.
string header = Catalog.GetPluralString ("Merge the selected tag",
"Merge the {0} selected tags?", tags.Length);
header = String.Format (header, tags.Length);
// If a tag with children tags is selected for merging, we
// should also merge its children..
List<Tag> all_tags = new List<Tag> (tags.Length);
foreach (Tag tag in tags) {
if (!all_tags.Contains (tag))
all_tags.Add (tag);
else
continue;
if (!(tag is Category))
continue;
(tag as Category).AddDescendentsTo (all_tags);
}
// debug..
tags = all_tags.ToArray ();
System.Array.Sort (tags, new TagRemoveComparer ());
foreach (Tag tag in tags) {
Log.Debug ("tag: {0}", tag.Name);
}
string msg = Catalog.GetString ("This operation will merge the selected tags and any sub-tags into a single tag.");
string ok_caption = Catalog.GetString ("_Merge Tags");
if (ResponseType.Ok != HigMessageDialog.RunHigConfirmation (main_window,
DialogFlags.DestroyWithParent,
MessageType.Warning,
header,
msg,
ok_caption))
return;
// The surviving tag is the last tag, as it is definitely not a child of any other the
// other tags. removetags will contain the tags to be merged.
Tag survivor = tags [tags.Length - 1];
var removetags = new Tag [tags.Length - 1];
Array.Copy (tags, 0, removetags, 0, tags.Length - 1);
// Add the surviving tag to all the photos with the other tags
var photos = Database.Photos.Query (removetags);
foreach (Photo p in photos) {
p.AddTag (survivor);
}
// Remove the defunct tags, which removes them from the photos, commits
// the photos, and removes the tags from the TagStore
Database.BeginTransaction ();
Database.Photos.Remove (removetags);
Database.CommitTransaction ();
HandleEditSelectedTagWithTag (survivor);
}