Esempio n. 1
0
        void create_mosaics()
        {
            //Prepare the query
            Db db = App.Instance.Database;

            FSpot.PhotoQuery mini_query = new FSpot.PhotoQuery(db.Photos);
            Photo []         photos;

            if (tags_radio.Active)
            {
                //Build tag array
                List <Tag> taglist = new List <Tag> ();
                foreach (string tag_name in miniatures_tags.GetTypedTagNames())
                {
                    Tag t = db.Tags.GetTagByName(tag_name);
                    if (t != null)
                    {
                        taglist.Add(t);
                    }
                }
                mini_query.Terms = FSpot.OrTerm.FromTags(taglist.ToArray());
                photos           = mini_query.Photos;
            }
            else
            {
                photos = App.Instance.Organizer.Query.Photos;
            }

            if (photos.Length == 0)
            {
                //There is no photo for the selected tags! :(
                InfoDialog(Catalog.GetString("No photos for the selection"),
                           Catalog.GetString("The tags selected provided no pictures. Please select different tags"),
                           Gtk.MessageType.Error);
                return;
            }

            //Create minis
            ProgressDialog progress_dialog = null;

            progress_dialog = new ProgressDialog(Catalog.GetString("Creating miniatures"),
                                                 ProgressDialog.CancelButtonType.Stop,
                                                 photos.Length, metapixel_dialog);

            minidir_tmp = System.IO.Path.GetTempFileName();
            System.IO.File.Delete(minidir_tmp);
            System.IO.Directory.CreateDirectory(minidir_tmp);
            minidir_tmp += "/";

            //Call MetaPixel to create the minis
            foreach (Photo p in photos)
            {
                if (progress_dialog.Update(String.Format(Catalog.GetString("Preparing photo \"{0}\""), p.Name)))
                {
                    progress_dialog.Destroy();
                    DeleteTmp();
                    return;
                }
                //FIXME should switch to retry/skip
                if (!GLib.FileFactory.NewForUri(p.DefaultVersion.Uri).Exists)
                {
                    Log.WarningFormat(String.Format("Couldn't access photo {0} while creating miniatures", p.DefaultVersion.Uri.LocalPath));
                    continue;
                }
                //FIXME Check if the picture's format is supproted (jpg, gif)

                FilterSet filters = new FilterSet();
                filters.Add(new JpegFilter());
                FilterRequest freq = new FilterRequest(p.DefaultVersion.Uri);
                filters.Convert(freq);

                //We use photo id for minis, instead of photo names, to avoid duplicates
                string minifile        = minidir_tmp + p.Id.ToString() + System.IO.Path.GetExtension(p.DefaultVersion.Uri.ToString());
                string prepare_command = String.Format("--prepare -w {0} -h {1} {2} {3} {4}tables.mxt",
                                                       icon_x_size.Text,                         //Minis width
                                                       icon_y_size.Text,                         //Minis height
                                                       GLib.Shell.Quote(freq.Current.LocalPath), //Source image
                                                       GLib.Shell.Quote(minifile),               //Dest image
                                                       minidir_tmp);                             //Table file
                Log.Debug("Executing: metapixel " + prepare_command);

                System.Diagnostics.Process mp_prep = System.Diagnostics.Process.Start("metapixel", prepare_command);
                mp_prep.WaitForExit();
                if (!System.IO.File.Exists(minifile))
                {
                    Log.DebugFormat("No mini? No party! {0}", minifile);
                    continue;
                }
            }             //Finished preparing!
            if (progress_dialog != null)
            {
                progress_dialog.Destroy();
            }

            progress_dialog = null;
            progress_dialog = new ProgressDialog(Catalog.GetString("Creating photomosaics"),
                                                 ProgressDialog.CancelButtonType.Stop,
                                                 App.Instance.Organizer.SelectedPhotos().Length, metapixel_dialog);

            //Now create the mosaics!
            uint error_count = 0;

            foreach (Photo p in App.Instance.Organizer.SelectedPhotos())
            {
                if (progress_dialog.Update(String.Format(Catalog.GetString("Processing \"{0}\""), p.Name)))
                {
                    progress_dialog.Destroy();
                    DeleteTmp();
                    return;
                }
                //FIXME should switch to retry/skip
                if (!GLib.FileFactory.NewForUri(p.DefaultVersion.Uri).Exists)
                {
                    Log.WarningFormat(String.Format("Couldn't access photo {0} while creating mosaics", p.DefaultVersion.Uri.LocalPath));
                    error_count++;
                    continue;
                }

                //FIXME Check if the picture's format is supproted (jpg, gif)

                FilterSet filters = new FilterSet();
                filters.Add(new JpegFilter());
                FilterRequest freq = new FilterRequest(p.DefaultVersion.Uri);
                filters.Convert(freq);

                string     name   = GetVersionName(p);
                System.Uri mosaic = GetUriForVersionName(p, name);

                string mosaic_command = String.Format("--metapixel -l {0} {1} {2}",
                                                      minidir_tmp,
                                                      GLib.Shell.Quote(freq.Current.LocalPath),
                                                      GLib.Shell.Quote(mosaic.LocalPath));
                Log.Debug("Executing: metapixel " + mosaic_command);
                System.Diagnostics.Process mp_exe = System.Diagnostics.Process.Start("metapixel", mosaic_command);
                mp_exe.WaitForExit();
                if (!GLib.FileFactory.NewForUri(mosaic).Exists)
                {
                    Log.Warning("Error in processing image " + p.Name);
                    error_count++;
                    continue;
                }

                p.DefaultVersionId    = p.AddVersion(mosaic, name, true);
                p.Changes.DataChanged = true;
                Core.Database.Photos.Commit(p);
            }             //Finished creating mosaics
            if (progress_dialog != null)
            {
                progress_dialog.Destroy();
            }


            string final_message = "Your mosaics have been generated as new versions of the pictures you selected";

            if (error_count > 0)
            {
                final_message += String.Format(".\n{0} images out of {1} had errors",
                                               error_count, App.Instance.Organizer.SelectedPhotos().Length);
            }
            InfoDialog(Catalog.GetString("PhotoMosaics generated!"),
                       Catalog.GetString(final_message),
                       (error_count == 0 ? Gtk.MessageType.Info : Gtk.MessageType.Warning));
            DeleteTmp();
        }
	public int ImportFromFile (PhotoStore store, string path)
	{
		this.store = store;
		this.CreateDialog ("import_dialog");
		
		this.Dialog.TransientFor = main_window;
		this.Dialog.WindowPosition = Gtk.WindowPosition.CenterOnParent;
		this.Dialog.Response += HandleDialogResponse;

	        AllowFinish = false;
		
		this.Dialog.DefaultResponse = ResponseType.Ok;
		
		//import_folder_entry.Activated += HandleEntryActivate;
		recurse_check.Toggled += HandleRecurseToggled;
		copy_check.Toggled += HandleRecurseToggled;

		menu = new SourceMenu (this);
		source_option_menu.Menu = menu;

		collection = new FSpot.PhotoList (new Photo [0]);
		tray = new FSpot.ScalingIconView (collection);
		tray.Selection.Changed += HandleTraySelectionChanged;
		icon_scrolled.SetSizeRequest (200, 200);
		icon_scrolled.Add (tray);
		//icon_scrolled.Visible = false;
		tray.DisplayTags = false;
		tray.Show ();

		photo_view = new FSpot.PhotoImageView (collection);
		photo_scrolled.Add (photo_view);
		photo_scrolled.SetSizeRequest (200, 200);
		photo_view.Show ();

		//FSpot.Global.ModifyColors (frame_eventbox);
		FSpot.Global.ModifyColors (photo_scrolled);
		FSpot.Global.ModifyColors (photo_view);

		photo_view.Pixbuf = PixbufUtils.LoadFromAssembly ("f-spot-48.png");
		photo_view.Fit = true;
			
		tag_entry = new FSpot.Widgets.TagEntry (MainWindow.Toplevel.Database.Tags, false);
		tag_entry.UpdateFromTagNames (new string []{});
		tagentry_box.Add (tag_entry);

		tag_entry.Show ();

		this.Dialog.Show ();
		//source_option_menu.Changed += HandleSourceChanged;
		if (path != null) {
			SetImportPath (path);
			int i = menu.FindItemPosition (path);

			if (i > 0) {
				source_option_menu.SetHistory ((uint)i);
			} else if (Directory.Exists (path)) {
				SourceItem path_item = new SourceItem (new VfsSource (path));
				menu.Prepend (path_item);
				path_item.ShowAll ();
				SetImportPath (path);
				source_option_menu.SetHistory (0);
			} 
			idle_start.Start ();
		}
						
		ResponseType response = (ResponseType) this.Dialog.Run ();
		
		while (response == ResponseType.Ok) {
			try {
				if (Directory.Exists (this.ImportPath))
					break;
			} catch (System.Exception e){
				System.Console.WriteLine (e);
				break;
			}

			HigMessageDialog md = new HigMessageDialog (this.Dialog,
			        DialogFlags.DestroyWithParent,
				MessageType.Error,
				ButtonsType.Ok,
				Catalog.GetString ("Directory does not exist."),
				String.Format (Catalog.GetString ("The directory you selected \"{0}\" does not exist.  " + 
								  "Please choose a different directory"), this.ImportPath));

			md.Run ();
			md.Destroy ();

			response = (Gtk.ResponseType) this.Dialog.Run ();
		}

		if (response == ResponseType.Ok) {
			this.UpdateTagStore (tag_entry.GetTypedTagNames ());
			this.Finish ();

			if (tags_selected != null && tags_selected.Count > 0) {
				for (int i = 0; i < collection.Count; i++) {
					Photo p = collection [i] as Photo;
					
					if (p == null)
						continue;
					
					p.AddTag ((Tag [])tags_selected.ToArray(typeof(Tag)));
					store.Commit (p);
				}
			}

			this.Dialog.Destroy ();
			return collection.Count;
		} else {
			this.Cancel ();
			//this.Dialog.Destroy();
			return 0;
		}
	}
    public int ImportFromFile(PhotoStore store, string path)
    {
        this.store = store;
        this.CreateDialog("import_dialog");

        this.Dialog.TransientFor   = main_window;
        this.Dialog.WindowPosition = Gtk.WindowPosition.CenterOnParent;
        this.Dialog.Response      += HandleDialogResponse;

        AllowFinish = false;

        this.Dialog.DefaultResponse = ResponseType.Ok;

        //import_folder_entry.Activated += HandleEntryActivate;
        recurse_check.Toggled += HandleRecurseToggled;
        copy_check.Toggled    += HandleRecurseToggled;

        menu = new SourceMenu(this);
        source_option_menu.Menu = menu;

        collection              = new FSpot.PhotoList(new Photo [0]);
        tray                    = new FSpot.ScalingIconView(collection);
        tray.Selection.Changed += HandleTraySelectionChanged;
        icon_scrolled.SetSizeRequest(200, 200);
        icon_scrolled.Add(tray);
        //icon_scrolled.Visible = false;
        tray.DisplayTags = false;
        tray.Show();

        photo_view = new FSpot.PhotoImageView(collection);
        photo_scrolled.Add(photo_view);
        photo_scrolled.SetSizeRequest(200, 200);
        photo_view.Show();

        //FSpot.Global.ModifyColors (frame_eventbox);
        FSpot.Global.ModifyColors(photo_scrolled);
        FSpot.Global.ModifyColors(photo_view);

        photo_view.Pixbuf = PixbufUtils.LoadFromAssembly("f-spot-48.png");
        photo_view.Fit    = true;

        tag_entry = new FSpot.Widgets.TagEntry(MainWindow.Toplevel.Database.Tags, false);
        tag_entry.UpdateFromTagNames(new string [] {});
        tagentry_box.Add(tag_entry);

        tag_entry.Show();

        this.Dialog.Show();
        //source_option_menu.Changed += HandleSourceChanged;
        if (path != null)
        {
            SetImportPath(path);
            int i = menu.FindItemPosition(path);

            if (i > 0)
            {
                source_option_menu.SetHistory((uint)i);
            }
            else if (Directory.Exists(path))
            {
                SourceItem path_item = new SourceItem(new VfsSource(path));
                menu.Prepend(path_item);
                path_item.ShowAll();
                SetImportPath(path);
                source_option_menu.SetHistory(0);
            }
            idle_start.Start();
        }

        ResponseType response = (ResponseType)this.Dialog.Run();

        while (response == ResponseType.Ok)
        {
            try {
                if (Directory.Exists(this.ImportPath))
                {
                    break;
                }
            } catch (System.Exception e) {
                System.Console.WriteLine(e);
                break;
            }

            HigMessageDialog md = new HigMessageDialog(this.Dialog,
                                                       DialogFlags.DestroyWithParent,
                                                       MessageType.Error,
                                                       ButtonsType.Ok,
                                                       Catalog.GetString("Directory does not exist."),
                                                       String.Format(Catalog.GetString("The directory you selected \"{0}\" does not exist.  " +
                                                                                       "Please choose a different directory"), this.ImportPath));

            md.Run();
            md.Destroy();

            response = (Gtk.ResponseType) this.Dialog.Run();
        }

        if (response == ResponseType.Ok)
        {
            this.UpdateTagStore(tag_entry.GetTypedTagNames());
            this.Finish();

            if (tags_selected != null && tags_selected.Count > 0)
            {
                for (int i = 0; i < collection.Count; i++)
                {
                    Photo p = collection [i] as Photo;

                    if (p == null)
                    {
                        continue;
                    }

                    p.AddTag((Tag [])tags_selected.ToArray(typeof(Tag)));
                    store.Commit(p);
                }
            }

            this.Dialog.Destroy();
            return(collection.Count);
        }
        else
        {
            this.Cancel();
            //this.Dialog.Destroy();
            return(0);
        }
    }