<commit_before>using FSpot.UI.Dialog;
using FSpot.Widgets;
using FSpot.Utils;
using FSpot.Import;
using Gtk;
using Hyena;
using System;
using System.Collections.Generic;
using Mono.Unix;

namespace FSpot.UI.Dialog
{
    public class ImportDialog : BuilderDialog
    {
        static readonly string select_folder_label = Catalog.GetString ("Choose Folder...");
        private ImportController Controller { get; set; }
        private TreeStore Sources { get; set; }

        private static Dictionary<string, ImportSource> history_sources = new Dictionary<string, ImportSource> ();

        [GtkBeans.Builder.Object] Button cancel_button;
        [GtkBeans.Builder.Object] Button import_button;
        [GtkBeans.Builder.Object] CheckButton copy_check;
        [GtkBeans.Builder.Object] CheckButton duplicate_check;
        [GtkBeans.Builder.Object] CheckButton recurse_check;
        [GtkBeans.Builder.Object] ComboBox sources_combo;
        [GtkBeans.Builder.Object] HBox tagentry_box;
        [GtkBeans.Builder.Object] HPaned import_hpaned;
        [GtkBeans.Builder.Object] ProgressBar progress_bar;
        [GtkBeans.Builder.Object] ScrolledWindow icon_scrolled;
        [GtkBeans.Builder.Object] ScrolledWindow photo_scrolled;

        private PhotoImageView photo_view;
        private TagEntry tag_entry;

        public ImportDialog (ImportController controller, Window parent) : base ("import.ui", "import_dialog")
        {
            Controller = controller;
            BuildUI (parent);
            ResetPreview ();
            LoadPreferences ();
            ScanSources ();
            ConnectEvents ();
        }

        void BuildUI (Window parent)
        {
            TransientFor = parent;
            WindowPosition = WindowPosition.CenterOnParent;

            photo_view = new PhotoImageView (Controller.Photos);
            photo_scrolled.Add (photo_view);
            photo_scrolled.SetSizeRequest (200, 200);
            photo_view.Show ();

            GtkUtil.ModifyColors (photo_scrolled);
            GtkUtil.ModifyColors (photo_view);

            var tray = new ScalingIconView (Controller.Photos);
            tray.Selection.Changed += (c) => {
                if (tray.Selection.Count > 0)
                    photo_view.Item.Index = tray.Selection.Ids[0];
            };
            icon_scrolled.Add (tray);
            tray.DisplayTags = false;
            tray.Show ();

            progress_bar.Hide ();

            import_button.Sensitive = false;

            tag_entry = new TagEntry (App.Instance.Database.Tags, false);
            tag_entry.UpdateFromTagNames (new string []{});
            tagentry_box.Add (tag_entry);
            tag_entry.Show ();
        }

        void ResetPreview ()
        {
            photo_view.Pixbuf = GtkUtil.TryLoadIcon (FSpot.Global.IconTheme, "f-spot", 128, (Gtk.IconLookupFlags)0);
            photo_view.ZoomFit (false);
        }

        void LoadPreferences ()
        {
            if (Preferences.Get<int> (Preferences.IMPORT_WINDOW_WIDTH) > 0) {
                Resize (Preferences.Get<int> (Preferences.IMPORT_WINDOW_WIDTH), Preferences.Get<int> (Preferences.IMPORT_WINDOW_HEIGHT));
            }

            if (FSpot.Preferences.Get<int> (Preferences.IMPORT_WINDOW_PANE_POSITION) > 0) {
                import_hpaned.Position = Preferences.Get<int> (Preferences.IMPORT_WINDOW_PANE_POSITION);
            }

            copy_check.Active = Controller.CopyFiles;
            recurse_check.Active = Controller.RecurseSubdirectories;
            duplicate_check.Active = Controller.DuplicateDetect;
        }

        void ScanSources ()
        {
            // Populates the source combo box
            Sources = new TreeStore (typeof(ImportSource), typeof(string), typeof(string), typeof(bool));
            sources_combo.Model = Sources;
            sources_combo.RowSeparatorFunc = (m, i) => (m.GetValue (i, 1) as string) == String.Empty;
            var render = new CellRendererPixbuf ();
            sources_combo.PackStart (render, false);
            sources_combo.SetAttributes (render, "icon-name", 2, "sensitive", 3);
            var render2 = new CellRendererText ();
            sources_combo.PackStart (render2, true);
            sources_combo.SetAttributes (render2, "text", 1, "sensitive", 3);

            GLib.Idle.Add (() => {
                PopulateSourceCombo (null);
                QueueDraw ();
                return false;
            });
        }

        void PopulateSourceCombo (ImportSource source_to_activate)
        {
            int activate_index = 0;
            sources_combo.Changed -= OnSourceComboChanged;
            Sources.Clear ();
            Sources.AppendValues (null, Catalog.GetString ("Choose Import source..."), String.Empty, false);
            Sources.AppendValues (null, select_folder_label, "folder", true);
            Sources.AppendValues (null, String.Empty, String.Empty);
            bool mount_added = false;
            foreach (var source in Controller.Sources) {
                if (source == source_to_activate) {
                    activate_index = Sources.IterNChildren ();
                }
                Sources.AppendValues (source, source.Name, source.IconName, true);
                mount_added = true;
            }
            if (!mount_added) {
                Sources.AppendValues (null, Catalog.GetString ("(No Cameras Detected)"), String.Empty, false);
            }

            if (history_sources.Count > 0) {
                Sources.AppendValues (null, String.Empty, String.Empty);
                foreach (var source in history_sources.Values) {
                    if (source == source_to_activate) {
                        activate_index = Sources.IterNChildren ();<commit_msg>Don't make initial import source insensitive.<commit_after>using FSpot.UI.Dialog;
using FSpot.Widgets;
using FSpot.Utils;
using FSpot.Import;
using Gtk;
using Hyena;
using System;
using System.Collections.Generic;
using Mono.Unix;

namespace FSpot.UI.Dialog
{
    public class ImportDialog : BuilderDialog
    {
        static readonly string select_folder_label = Catalog.GetString ("Choose Folder...");
        private ImportController Controller { get; set; }
        private TreeStore Sources { get; set; }

        private static Dictionary<string, ImportSource> history_sources = new Dictionary<string, ImportSource> ();

        [GtkBeans.Builder.Object] Button cancel_button;
        [GtkBeans.Builder.Object] Button import_button;
        [GtkBeans.Builder.Object] CheckButton copy_check;
        [GtkBeans.Builder.Object] CheckButton duplicate_check;
        [GtkBeans.Builder.Object] CheckButton recurse_check;
        [GtkBeans.Builder.Object] ComboBox sources_combo;
        [GtkBeans.Builder.Object] HBox tagentry_box;
        [GtkBeans.Builder.Object] HPaned import_hpaned;
        [GtkBeans.Builder.Object] ProgressBar progress_bar;
        [GtkBeans.Builder.Object] ScrolledWindow icon_scrolled;
        [GtkBeans.Builder.Object] ScrolledWindow photo_scrolled;

        private PhotoImageView photo_view;
        private TagEntry tag_entry;

        public ImportDialog (ImportController controller, Window parent) : base ("import.ui", "import_dialog")
        {
            Controller = controller;
            BuildUI (parent);
            ResetPreview ();
            LoadPreferences ();
            ScanSources ();
            ConnectEvents ();
        }

        void BuildUI (Window parent)
        {
            TransientFor = parent;
            WindowPosition = WindowPosition.CenterOnParent;

            photo_view = new PhotoImageView (Controller.Photos);
            photo_scrolled.Add (photo_view);
            photo_scrolled.SetSizeRequest (200, 200);
            photo_view.Show ();

            GtkUtil.ModifyColors (photo_scrolled);
            GtkUtil.ModifyColors (photo_view);

            var tray = new ScalingIconView (Controller.Photos);
            tray.Selection.Changed += (c) => {
                if (tray.Selection.Count > 0)
                    photo_view.Item.Index = tray.Selection.Ids[0];
            };
            icon_scrolled.Add (tray);
            tray.DisplayTags = false;
            tray.Show ();

            progress_bar.Hide ();

            import_button.Sensitive = false;

            tag_entry = new TagEntry (App.Instance.Database.Tags, false);
            tag_entry.UpdateFromTagNames (new string []{});
            tagentry_box.Add (tag_entry);
            tag_entry.Show ();
        }

        void ResetPreview ()
        {
            photo_view.Pixbuf = GtkUtil.TryLoadIcon (FSpot.Global.IconTheme, "f-spot", 128, (Gtk.IconLookupFlags)0);
            photo_view.ZoomFit (false);
        }

        void LoadPreferences ()
        {
            if (Preferences.Get<int> (Preferences.IMPORT_WINDOW_WIDTH) > 0) {
                Resize (Preferences.Get<int> (Preferences.IMPORT_WINDOW_WIDTH), Preferences.Get<int> (Preferences.IMPORT_WINDOW_HEIGHT));
            }

            if (FSpot.Preferences.Get<int> (Preferences.IMPORT_WINDOW_PANE_POSITION) > 0) {
                import_hpaned.Position = Preferences.Get<int> (Preferences.IMPORT_WINDOW_PANE_POSITION);
            }

            copy_check.Active = Controller.CopyFiles;
            recurse_check.Active = Controller.RecurseSubdirectories;
            duplicate_check.Active = Controller.DuplicateDetect;
        }

        void ScanSources ()
        {
            // Populates the source combo box
            Sources = new TreeStore (typeof(ImportSource), typeof(string), typeof(string), typeof(bool));
            sources_combo.Model = Sources;
            sources_combo.RowSeparatorFunc = (m, i) => (m.GetValue (i, 1) as string) == String.Empty;
            var render = new CellRendererPixbuf ();
            sources_combo.PackStart (render, false);
            sources_combo.SetAttributes (render, "icon-name", 2, "sensitive", 3);
            var render2 = new CellRendererText ();
            sources_combo.PackStart (render2, true);
            sources_combo.SetAttributes (render2, "text", 1, "sensitive", 3);

            GLib.Idle.Add (() => {
                PopulateSourceCombo (null);
                QueueDraw ();
                return false;
            });
        }

        void PopulateSourceCombo (ImportSource source_to_activate)
        {
            int activate_index = 0;
            sources_combo.Changed -= OnSourceComboChanged;
            Sources.Clear ();
            Sources.AppendValues (null, Catalog.GetString ("Choose Import source..."), String.Empty, true);
            Sources.AppendValues (null, select_folder_label, "folder", true);
            Sources.AppendValues (null, String.Empty, String.Empty);
            bool mount_added = false;
            foreach (var source in Controller.Sources) {
                if (source == source_to_activate) {
                    activate_index = Sources.IterNChildren ();
                }
                Sources.AppendValues (source, source.Name, source.IconName, true);
                mount_added = true;
            }
            if (!mount_added) {
                Sources.AppendValues (null, Catalog.GetString ("(No Cameras Detected)"), String.Empty, false);
            }

            if (history_sources.Count > 0) {
                Sources.AppendValues (null, String.Empty, String.Empty);
                foreach (var source in history_sources.Values) {
                    if (source == source_to_activate) {
                        activate_index = Sources.IterNChildren ();
{'fraction_non_alphanumeric': 0.09698331748638603, 'fraction_numerical': 0.004149018929898868, 'mean_word_length': 2.127872397945391, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3841361', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3042, 'n_words': 651}