Esempio n. 1
0
        private void okButton_Click(object sender, EventArgs e)
        {
            if (siteNameTextBox.Text == string.Empty)
            {
                siteNameTextBox.Focus();
                _tooltip.SetToolTip(siteNameTextBox, "Error");
                _tooltip.Show("You must specify a name.", siteNameTextBox, 3000);
                //_tooltip.RemoveAll();
                return;
            }
            else if (_parent.SearchEngines.Find(delegate(SearchEngine se) { return(se.Name == siteNameTextBox.Text); }) != null)
            {
                siteNameTextBox.Focus();
                _tooltip.SetToolTip(siteNameTextBox, "Error");
                _tooltip.Show("A website with this name alreay exists. Please specify a different one.", siteNameTextBox, 3000);
                //_tooltip.RemoveAll();
                return;
            }
            else if (siteUrlTextBox.Text == string.Empty)
            {
                siteUrlTextBox.Focus();
                _tooltip.SetToolTip(siteUrlTextBox, "Error");
                _tooltip.Show("You must specify a URL.", siteUrlTextBox, 3000);
                //_tooltip.RemoveAll();
                return;
            }
            else if (!_parent.UrlRegex.IsMatch(siteUrlTextBox.Text))
            {
                siteUrlTextBox.Focus();
                _tooltip.SetToolTip(siteUrlTextBox, "Error");
                _tooltip.Show("This is not a valid URL.", siteUrlTextBox, 3000);
                //_tooltip.RemoveAll();
                return;
            }
            else if (siteQueryTextBox.Text != string.Empty)
            {
                if (!_parent.UrlRegex.IsMatch(siteQueryTextBox.Text))
                {
                    siteQueryTextBox.Focus();
                    _tooltip.SetToolTip(siteQueryTextBox, "Error");
                    _tooltip.Show("This is not a valid search query.", siteQueryTextBox, 3000);
                    //_tooltip.RemoveAll();
                    return;
                }
                else if (!siteQueryTextBox.Text.Contains(SearchEngine.SearchTermToken))
                {
                    siteQueryTextBox.Focus();
                    _tooltip.SetToolTip(siteQueryTextBox, "Error");
                    _tooltip.Show("The search query must have a search term ('" + SearchEngine.SearchTermToken + "').", siteQueryTextBox, 3000);
                    //_tooltip.RemoveAll();
                    return;
                }
            }
            else if (siteIconTextBox.Text != string.Empty)
            {
                if (!_parent.UrlRegex.IsMatch(siteIconTextBox.Text))
                {
                    siteIconTextBox.Focus();
                    _tooltip.SetToolTip(siteIconTextBox, "Error");
                    _tooltip.Show("This is not a valid icon url.", siteIconTextBox, 3000);
                    //_tooltip.RemoveAll();
                    return;
                }
            }

            SearchEngine new_engine = new SearchEngine(siteNameTextBox.Text, siteUrlTextBox.Text, siteQueryTextBox.Text, siteIconTextBox.Text);

            _parent.SearchEngines.Add(new_engine);

            Command new_command = new Command(siteNameTextBox.Text);

            new_command.SetIsOwnerDelegate(new Command.OwnershipDelegate(delegate(string parameters)
            {
                return(true);
            }));
            new_command.SetNameDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                if (parameters.Trim() == string.Empty)
                {
                    return(new_engine.Name);
                }
                else
                {
                    return(new_engine.Name + " " + parameters);
                }
            }));
            new_command.SetDescriptionDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                if (parameters == string.Empty || new_engine.SearchQuery == string.Empty) // no search term or no search query was specified
                {
                    return("Go to " + new_engine.Name + " web page.");
                }
                else
                {
                    return("Search for " + parameters + " on " + new_engine.Name);
                }
            }));
            new_command.SetAutoCompleteDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                if (parameters == string.Empty)
                {
                    return(new_engine.Name);
                }
                else
                {
                    return(new_engine.Name + " " + parameters);
                }
            }));
            new_command.SetIconDelegate(new Command.IconDelegate(delegate(string parameters)
            {
                return(_parent.IconCache.GetIcon(new_engine));
            }));
            new_command.SetUsageDelegate(new Command.UsageDelegate(delegate(string parameters)
            {
                List <string> args             = new List <string>();
                Dictionary <string, bool> comp = new Dictionary <string, bool>();

                args.Add("term");
                if (parameters != string.Empty)
                {
                    comp.Add("term", true);
                }
                else
                {
                    comp.Add("term", false);
                }

                return(new CommandUsage(new_command.Name, args, comp));
            }));
            new_command.SetExecuteDelegate(new Command.ExecutionDelegate(delegate(string parameters, Keys modifiers)
            {
                try
                {
                    if (parameters == string.Empty) // no search term
                    {
                        System.Diagnostics.Process.Start(new_engine.Url);
                    }
                    else
                    {
                        System.Diagnostics.Process.Start(new_engine.GetSearchQuery(parameters));
                    }
                }
                catch
                {
                }
            }));

            _parent.Commands.Add(new_command);
            DialogResult = DialogResult.OK;
            Close();
        }
Esempio n. 2
0
        //private void LoadDefaultEngines()
        //{
        //    _search_engines = new List<SearchEngine>();
        //    _search_engine_names = new List<string>();
        //    //// add search engines
        //    // Google
        //    _search_engine_names.Add("Google");
        //    _search_engines.Add(new SearchEngine("Google",
        //                                        @"http://www.google.com/ncr",
        //                                        @"http://www.google.com/search?hl=en&q=%s&btnG=Google+Search&aq=f&oq=",
        //                                        @"http://www.iconarchive.com/icons/fasticon/web-2/32/Google-icon.png"));
        //    _favorite_engine = 0;
        //    // Youtube
        //    _search_engine_names.Add("YouTube");
        //    _search_engines.Add(new SearchEngine("YouTube",
        //                                        @"http://www.youtube.com/",
        //                                        @"http://www.youtube.com/results?search_query=%s",
        //                                        @"http://www.iconarchive.com/icons/fasticon/web-2/32/Youtube-icon.png"));
        //    // Wikipedia
        //    _search_engine_names.Add("Wikipedia");
        //    _search_engines.Add(new SearchEngine("Wikipedia",
        //                                        @"http://www.wikipedia.org/",
        //                                        @"http://en.wikipedia.org/wiki/Special:Search?search=%s&go=Go",
        //                                        @"http://www.iconarchive.com/icons/sykonist/popular-sites/32/Wikipedia-globe-icon.png"));
        //    // IMDB
        //    _search_engine_names.Add("IMDB");
        //    _search_engines.Add(new SearchEngine("IMDB",
        //                                        @"http://www.imdb.com/",
        //                                        @"http://www.imdb.com/find?s=all&q=%s",
        //                                        @"http://www.iconarchive.com/icons/iconshock/cinema/32/film-reel-icon.png"));
        //    // Yahoo
        //    _search_engine_names.Add("Yahoo");
        //    _search_engines.Add(new SearchEngine("Yahoo",
        //                                        @"http://search.yahoo.com/",
        //                                        @"http://search.yahoo.com/search?p=%s",
        //                                        @"http://www.iconarchive.com/icons/deleket/puck/32/Yahoo-Messenger-Alternate-icon.png"));
        //    // Weather
        //    _search_engine_names.Add("Weather");
        //    _search_engines.Add(new SearchEngine("Weather",
        //                                        @"http://www.weather.com/",
        //                                        @"http://www.weather.com/weather/today/%s",
        //                                        @"http://www.iconarchive.com/icons/icons-land/weather/32/Snow-Occasional-icon.png"));
        //    // Maps
        //    _search_engine_names.Add("Maps");
        //    _search_engines.Add(new SearchEngine("Maps",
        //                                        @"http://maps.google.com/",
        //                                        @"http://maps.google.com/maps?f=q&hl=en&geocode=&q=%s&ie=UTF8&z=12&iwloc=addr&om=1",
        //                                        @"http://www.iconarchive.com/icons/walrick/openphone/32/Maps-icon.png"));
        //    // Bing
        //    _search_engine_names.Add("Bing");
        //    _search_engines.Add(new SearchEngine("Bing",
        //                                        @"http://www.bing.com/",
        //                                        @"http://www.bing.com/search?q=%s",
        //                                        @"http://www.bing.com/"));
        //    // Dictionary
        //    _search_engine_names.Add("Dictionary");
        //    _search_engines.Add(new SearchEngine("Dictionary",
        //                                        @"http://www.dictionary.com/",
        //                                        @"http://www.dictionary.com/browse/%s",
        //                                        @"http://www.iconarchive.com/icons/dimension-of-deskmod/micro/32/Dictionary-icon.png"));
        //    // Thesaurus
        //    _search_engine_names.Add("Thesaurus");
        //    _search_engines.Add(new SearchEngine("Thesaurus",
        //                                        @"http://thesaurus.com/",
        //                                        @"http://thesaurus.reference.com/browse/%s",
        //                                        @"http://www.iconarchive.com/icons/dimension-of-deskmod/micro/32/Dictionary-icon.png"));
        //    // MSDN
        //    _search_engine_names.Add("MSDN");
        //    _search_engines.Add(new SearchEngine("MSDN",
        //                                        @"http://msdn.microsoft.com/",
        //                                        @"http://search.msdn.microsoft.com/search/default.aspx?siteId=0&tab=0&query=%s",
        //                                        @"http://msdn.microsoft.com/"));
        //    // Amazon
        //    _search_engine_names.Add("Amazon");
        //    _search_engines.Add(new SearchEngine("Amazon",
        //                                        @"http://www.amazon.com/",
        //                                        @"http://www.amazon.com/gp/search?keywords=%s&index=blended",
        //                                        @"http://www.amazon.com/"));
        //    // Facebook
        //    _search_engine_names.Add("Facebook");
        //    _search_engines.Add(new SearchEngine("Facebook",
        //                                        @"http://www.facebook.com/",
        //                                        @"http://www.facebook.com/search/?ref=search&q=%s",
        //                                        @"http://www.iconarchive.com/icons/fasticon/web-2/32/FaceBook-icon.png"));
        //    // Twitter
        //    _search_engine_names.Add("Twitter");
        //    _search_engines.Add(new SearchEngine("Twitter",
        //                                        @"http://twitter.com/",
        //                                        @"http://twitter.com/#search?q=%s",
        //                                        @"http://www.iconarchive.com/icons/fasticon/web-2/32/Twitter-icon.png"));
        //    // Flickr
        //    _search_engine_names.Add("Flickr");
        //    _search_engines.Add(new SearchEngine("Flickr",
        //                                        @"http://www.flickr.com/",
        //                                        @"http://www.flickr.com/search/?q=%s",
        //                                        @"http://www.iconarchive.com/icons/fasticon/web-2/32/Flickr-icon.png"));
        //}
        #endregion

        #region Overrided Methods
        protected override void SetupCommands()
        {
            LoadSettings();
            // create search commands
            // problema com os delegates e ciclos: http://decav.com/blogs/andre/archive/2007/11/18/wtf-quot-problems-quot-with-anonymous-delegates-linq-lambdas-and-quot-foreach-quot-or-quot-for-quot-loops.aspx
            foreach (SearchEngine engine in SearchEngines)
            {
                SearchEngine eng = new SearchEngine(engine);
                Command      cmd;
                if (eng.Name != SearchEngines[FavoriteEngine].Name)
                {
                    cmd = new Command(eng.Name);
                }
                else
                {
                    cmd = new Command(eng.Name, Command.PriorityType.Medium | Command.PriorityType.Low);
                }

                cmd.SetIsOwnerDelegate(new Command.OwnershipDelegate(delegate(string parameters)
                {
                    return(true);
                }));
                cmd.SetNameDelegate(new Command.EvaluationDelegate(delegate(string parameters)
                {
                    if (parameters.Trim() == string.Empty)
                    {
                        return(eng.Name);
                    }
                    else
                    {
                        return(eng.Name + " " + parameters);
                    }
                }));
                cmd.SetDescriptionDelegate(new Command.EvaluationDelegate(delegate(string parameters)
                {
                    if (parameters == string.Empty || eng.SearchQuery == string.Empty) // no search term or no search query was specified
                    {
                        return("Go to " + eng.Name + " web page.");
                    }
                    else
                    {
                        return("Search for " + parameters + " on " + eng.Name);
                    }
                }));
                cmd.SetAutoCompleteDelegate(new Command.EvaluationDelegate(delegate(string parameters)
                {
                    if (parameters == string.Empty)
                    {
                        return(eng.Name);
                    }
                    else
                    {
                        return(eng.Name + " " + parameters);
                    }
                }));
                cmd.SetIconDelegate(new Command.IconDelegate(delegate(string parameters)
                {
                    //return _search_icon.ToBitmap();
                    return(_icon_cache.GetIcon(eng));
                }));
                cmd.SetUsageDelegate(new Command.UsageDelegate(delegate(string parameters)
                {
                    List <string> args             = new List <string>();
                    Dictionary <string, bool> comp = new Dictionary <string, bool>();

                    args.Add("term");
                    if (parameters != string.Empty)
                    {
                        comp.Add("term", true);
                    }
                    else
                    {
                        comp.Add("term", false);
                    }

                    return(new CommandUsage(cmd.Name, args, comp));
                }));
                cmd.SetExecuteDelegate(new Command.ExecutionDelegate(delegate(string parameters, Keys modifiers)
                {
                    try
                    {
                        if (parameters == string.Empty) // no search term
                        {
                            System.Diagnostics.Process.Start(eng.Url);
                        }
                        else
                        {
                            System.Diagnostics.Process.Start(eng.GetSearchQuery(parameters));
                        }
                    }
                    catch
                    {
                    }
                }));
                _commands.Add(cmd);
            }

            //_add_command = new Command("Add New Website", new string[] { "add", "new", "website", "search", "engine" }, "Add a new search engine.", Command.PriorityType.Medium);
            _add_command = new Command("Add New Website", "Add a new search engine.", Command.PriorityType.Medium);
            _add_command.SetIsOwnerDelegate(new Command.OwnershipDelegate(delegate(string parameters)
            {
                return(false);
            }));
            _add_command.SetNameDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(_add_command.Name);
            }));
            _add_command.SetDescriptionDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(_add_command.Description);
            }));
            _add_command.SetAutoCompleteDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(_add_command.Name);
            }));
            _add_command.SetIconDelegate(new Command.IconDelegate(delegate(string parameters)
            {
                return(_add_icon.ToBitmap());
            }));
            _add_command.SetUsageDelegate(new Command.UsageDelegate(delegate(string parameters)
            {
                List <string> args             = new List <string>();
                Dictionary <string, bool> comp = new Dictionary <string, bool>();

                return(new CommandUsage(_add_command.Name, args, comp));
            }));
            _add_command.SetExecuteDelegate(new Command.ExecutionDelegate(delegate(string parameters, Keys modifiers)
            {
                EnginePicker ep = new EnginePicker(this);
                if (ep.ShowDialog() == DialogResult.OK)
                {
                    SaveSettings();
                }
                ep.Dispose();
            }));

            _process_url_command = new Command("Process URL", Command.PriorityType.High);
            _process_url_command.SetIsOwnerDelegate(new Command.OwnershipDelegate(delegate(string parameters)
            {
                //Regex regex = new Regex(@"^(((h|H)(t|T)(t|T)(p|P)(s?|S?))\://)?(www.|[a-zA-Z0-9].)[a-zA-Z0-9\-\.]+\..*$");
                return(_regex.IsMatch(parameters));
            }));
            _process_url_command.SetNameDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(parameters);
            }));
            _process_url_command.SetDescriptionDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return("Browse to " + parameters);
            }));
            _process_url_command.SetAutoCompleteDelegate(new Command.EvaluationDelegate(delegate(string parameters)
            {
                return(parameters);
            }));
            _process_url_command.SetIconDelegate(new Command.IconDelegate(delegate(string parameters)
            {
                return(_browser_icon.ToBitmap());
            }));
            _process_url_command.SetUsageDelegate(new Command.UsageDelegate(delegate(string parameters)
            {
                List <string> args             = new List <string>();
                Dictionary <string, bool> comp = new Dictionary <string, bool>();

                return(new CommandUsage(@"Valid URL. i.e.: http://www.google.com", args, comp));
            }));
            _process_url_command.SetExecuteDelegate(new Command.ExecutionDelegate(delegate(string parameters, Keys modifiers)
            {
                try
                {
                    System.Diagnostics.Process.Start(FixUrl(parameters));
                }
                catch
                {
                }
            }));

            _commands.Add(_add_command);
            _commands.Add(_process_url_command);
        }