Example #1
0
        public form_main()
        {
            InitializeComponent();

            // get user name in order to get the preferences
            user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            user = user.Replace(user.Substring(0, user.LastIndexOf(@"\") + 1), "");

            // get app folder location
            mainDirectory_path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).Replace("ezFileSearch\\bin\\Debug", "");

            // try to get user details
            string json_text      = string.Empty;
            string user_data_path = mainDirectory_path + @"\" + "Data" + @"\" + user + ".json";

            // get editors path
            atom_path    = mainDirectory_path + "\\Editors\\AtomPortable\\AtomPortable.exe";
            sublime_path = mainDirectory_path + "\\Editors\\SublimePortable\\sublime_text";
            //notepad++_path not needed, not portable

            try
            {
                json_text = File.ReadAllText(user_data_path);
                JsonTextReader reader = new JsonTextReader(new StringReader(json_text));
                JObject        o      = (JObject)JToken.ReadFrom(reader);

                object test = o["servers"];

                foreach (var data in o["servers"])
                {
                    tb_serverList.AppendText(data + Environment.NewLine);
                }

                foreach (var data in o["locations"])
                {
                    tb_locationList.AppendText(data + Environment.NewLine);
                }

                foreach (var data in o["patterns"])
                {
                    tb_patternList.AppendText(data + Environment.NewLine);
                }

                tb_text.Text    = (string)o["text"];
                tb_newText.Text = (string)o["newText"];

                cb_editor.SelectedIndex = ((JToken)o["editorIdx"]).ToObject <int>();
            }
            catch
            {
                // create user json and directory if does not exists
                FileInfo file = new FileInfo(user_data_path);
                file.Directory.Create();
                File.WriteAllText(user_data_path, "{\n}");
            }

            // init global variables
            SR     = new ez_SearchResult();
            Sender = new ez_Sender();
        }
Example #2
0
        private void findFiles(object sender, DoWorkEventArgs e)
        {
            // take input from UI
            string[] servers   = tb_serverList.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            string[] locations = tb_locationList.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            string[] patterns  = tb_patternList.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            bool   textFilter = false;
            string text       = string.Empty;

            this.tb_text.Invoke(new MethodInvoker(delegate
            {
                if (tb_text.Text != string.Empty)
                {
                    textFilter = true;
                }
            }));
            this.tb_text.Invoke(new MethodInvoker(delegate
            {
                text = tb_text.Text;
            }));

            // remove empty lines
            servers   = servers.Where(val => !string.IsNullOrEmpty(val)).Distinct().ToArray();
            locations = locations.Where(val => !string.IsNullOrEmpty(val)).Distinct().ToArray();
            patterns  = patterns.Where(val => !string.IsNullOrEmpty(val)).Distinct().ToArray();

            // delete everything from last search
            SR = new ez_SearchResult();
            this.panel_dynamic.Invoke(new MethodInvoker(delegate
            {
                panel_dynamic.Controls.Clear();
            }));

            // disable scrollbar while generating UI elements (bug workaround)
            this.panel_dynamic.Invoke(new MethodInvoker(delegate
            {
                panel_dynamic.VerticalScroll.Enabled = false;
                panel_dynamic.AutoScroll             = false;
            }));

            int x = 0, y = 6, x_spacing = 15, y_spacing = 33;

            // button for opening all files
            Button b_all = new Button();

            b_all.Location  = new Point(x, y);
            b_all.Name      = "btn_OpenAllFiles";
            b_all.AutoSize  = true;
            b_all.ForeColor = Color.White;
            b_all.Text      = "open all";
            b_all.Font      = new Font("Microsoft Sans Serif", 12f);
            b_all.Click    += new EventHandler(openFiles_click);
            this.panel_dynamic.Invoke(new MethodInvoker(delegate
            {
                panel_dynamic.Controls.Add(b_all);
            }));
            // increment y
            y += y_spacing;

            // find all files
            foreach (string server in servers)
            {
                Button b_srv = new Button();
                b_srv.Location  = new Point(x + x_spacing, y);
                b_srv.Name      = serverTag + server;
                b_srv.Text      = server;
                b_srv.AutoSize  = true;
                b_srv.ForeColor = Color.White;
                b_srv.Text      = server;
                b_srv.Font      = new Font("Microsoft Sans Serif", 12f);
                b_srv.Click    += new EventHandler(openFiles_click);
                this.panel_dynamic.Invoke(new MethodInvoker(delegate
                {
                    panel_dynamic.Controls.Add(b_srv);
                }));
                // increment y
                y += y_spacing;

                // create ez_Srv and save info
                ez_Srv ez_server = new ez_Srv();
                ez_server.btn = b_srv;

                foreach (string location in locations)
                {
                    Button b_loc = new Button();
                    b_loc.Location  = new Point(x + 2 * x_spacing, y);
                    b_loc.Name      = serverTag + server + locationTag + location;
                    b_loc.Text      = location;
                    b_loc.AutoSize  = true;
                    b_loc.ForeColor = Color.White;
                    b_loc.Font      = new Font("Microsoft Sans Serif", 12f);
                    b_loc.Click    += new EventHandler(openFiles_click);
                    this.panel_dynamic.Invoke(new MethodInvoker(delegate
                    {
                        panel_dynamic.Controls.Add(b_loc);
                    }));
                    // increment y
                    y += y_spacing;

                    // create ez_Loc and save it to our ez_Srv
                    ez_Loc ez_location = new ez_Loc();
                    ez_location.btn = b_loc;
                    ez_server.locations.Add(ez_location);

                    try
                    {
                        //local host or server?
                        string path;
                        if (server != "localhost" && server != "local")
                        {
                            path = @"\\" + server + @"\" + location;
                            path = path.Replace(":", "$");
                        }
                        else
                        {
                            path = location;
                        }

                        // save math in ez_Loc
                        ez_location.path = path;

                        DirectoryInfo dir = new DirectoryInfo(path);

                        List <FileInfo> files = new List <FileInfo>();

                        // get all files
                        foreach (string pattern in patterns)
                        {
                            List <FileInfo> fi = dir.GetFiles(pattern).ToList();
                            files.AddRange(fi);
                        }

                        if (files.Count() == 0)
                        {
                            // msg: no files in server -> location matching the patterns
                        }
                        else
                        {
                            // now, foreach file do the shit
                            foreach (var file in files)
                            {
                                if (textFilter)
                                {
                                    if (findTextInFile(file.FullName, text))
                                    {
                                        // create button and checkbox
                                        Button b = new Button();
                                        b.Text      = file.Name;
                                        b.Name      = serverTag + server + locationTag + location + fileTag + file.Name;
                                        b.AutoSize  = true;
                                        b.ForeColor = Color.White;
                                        b.Font      = new Font("Microsoft Sans Serif", 12f);
                                        b.Click    += new EventHandler(openFiles_click);

                                        /*CheckBox c = new CheckBox();
                                         * c.Checked = true;
                                         * c.AutoSize = true;
                                         * c.CheckedChanged += new EventHandler(checkedChanged_click);
                                         *
                                         * // set x si y for checkbox and button
                                         * c.Location = new Point(x + 3*x_spacing, y + y_spacing/4);*/
                                        b.Location = new Point(x + 3 * x_spacing, y); // *4 if checkbox

                                        // increment y
                                        y += y_spacing;

                                        this.panel_dynamic.Invoke(new MethodInvoker(delegate
                                        {
                                            //panel_dynamic.Controls.Add(c);
                                        }));
                                        this.panel_dynamic.Invoke(new MethodInvoker(delegate
                                        {
                                            panel_dynamic.Controls.Add(b);
                                        }));

                                        // create ez_File and save it to our ez_Loc from our ez_Srv
                                        ez_File ez_file = new ez_File();
                                        ez_file.fi  = new FileInfo(file.Name);
                                        ez_file.btn = b;
                                        //ez_file.cb = c;
                                        ez_location.files.Add(ez_file);
                                    }
                                }
                                else
                                {
                                    // create button and checkbox
                                    Button b = new Button();
                                    b.Text      = file.Name;
                                    b.Name      = serverTag + server + locationTag + location + fileTag + file.Name;
                                    b.AutoSize  = true;
                                    b.ForeColor = Color.White;
                                    b.Font      = new Font("Microsoft Sans Serif", 12f);
                                    b.Click    += new EventHandler(openFiles_click);

                                    /*CheckBox c = new CheckBox();
                                     * c.Checked = true;
                                     * c.AutoSize = true;
                                     * c.CheckedChanged += new EventHandler(checkedChanged_click);
                                     *
                                     * // set x si y for checkbox and button
                                     * c.Location = new Point(x + 3*x_spacing, y + y_spacing/4);*/
                                    b.Location = new Point(x + 3 * x_spacing, y); // *4 if checkbox

                                    // increment y
                                    y += y_spacing;

                                    this.panel_dynamic.Invoke(new MethodInvoker(delegate
                                    {
                                        //panel_dynamic.Controls.Add(c);
                                    }));
                                    this.panel_dynamic.Invoke(new MethodInvoker(delegate
                                    {
                                        panel_dynamic.Controls.Add(b);
                                    }));

                                    // create ez_File and save it to our ez_Loc from our ez_Srv
                                    ez_File ez_file = new ez_File();
                                    ez_file.fi  = new FileInfo(file.Name);
                                    ez_file.btn = b;
                                    //ez_file.cb = c;
                                    ez_location.files.Add(ez_file);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw;
                        // msg: no directory
                    }
                }
                SR.servers.Add(ez_server);
            }

            // enable scrollbar after generating UI elements (bug workaround)
            this.panel_dynamic.Invoke(new MethodInvoker(delegate
            {
                panel_dynamic.VerticalScroll.Enabled = true;
                panel_dynamic.AutoScroll             = true;
            }));
        }