// Helper method to do the search on a background thread
        internal void DoSimpleSearch(object arg)
        {
            SearchCondition searchCondition = SearchConditionFactory.ParseStructuredQuery(arg.ToString());
            ShellSearchFolder searchFolder =
                new ShellSearchFolder(searchCondition, (ShellContainer)BeforeSearchFolder);


            Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(
                    delegate()
                    {
                        Explorer.Navigate(searchFolder);

                    }
                ));

            if (BeforeSearchFolder != null)
            {
                Thread.Sleep(750);
                Explorer.RefreshExplorer();
            }


        }
        private void DoAdvancedSearch()
        {

            // This is our final searchcondition that we'll create the search folder from
            SearchCondition finalSearchCondition = null;

            // This is Prop1 + prop2 search condition... if the user didn't specify one of the properties,
            // we can just use the one property/value they specify...if they do, then we can do the and/or operation
            SearchCondition combinedPropSearchCondition = null;

            // Because we are doing the search on a background thread,
            // we can't access the UI controls from that thread.
            // Invoke from the main UI thread and get the values
            string prop1TextBox_Text = string.Empty;
            string prop2TextBox_Text = string.Empty;
            string prop1ComboBox_Value = string.Empty;
            string prop2ComboBox_Value = string.Empty;
            SearchConditionOperation prop1ConditionOperation = SearchConditionOperation.ValueContains;
            SearchConditionOperation prop2ConditionOperation = SearchConditionOperation.ValueContains;
            string prop1prop2OperationComboBox_Value = string.Empty;
            string comboBoxDateCreated_Value = string.Empty;
            int prop1prop2OperationComboBox_SelectedIndex = 0;
            bool dateSelected = false;
            List<ShellContainer> scopes = new List<ShellContainer>();

            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(
                delegate()
                {
                    prop1TextBox_Text = prop1TextBox.Text;
                    prop1TextBox_Text = prop1TextBox.Text;
                    prop1ComboBox_Value = prop1ComboBox.SelectedItem.ToString();
                    prop2ComboBox_Value = prop2ComboBox.SelectedItem.ToString();
                    prop1ConditionOperation = GetConditionOperation(prop1OperationComboBox);
                    prop2ConditionOperation = GetConditionOperation(prop2OperationComboBox);
                    prop1prop2OperationComboBox_Value = prop1prop2OperationComboBox.SelectedItem.ToString();
                    prop1prop2OperationComboBox_SelectedIndex = prop1prop2OperationComboBox.SelectedIndex;
                    comboBoxDateCreated_Value = comboBoxDateCreated.SelectedItem.ToString();
                    dateSelected = (comboBoxDateCreated.SelectedItem != dateCreatedNone);

                    foreach (StackPanel sp in locationsListBox.Items)
                    {
                        if (sp.Tag is ShellContainer)
                            scopes.Add((ShellContainer)sp.Tag);
                    }
                }));

            // If we have a valid first property/value, then create a search condition
            if (!string.IsNullOrEmpty(prop1TextBox_Text))
            {
                SearchCondition prop1Condition = SearchConditionFactory.CreateLeafCondition(
                    GetSearchProperty(prop1ComboBox_Value),
                    prop1TextBox_Text,
                    prop1ConditionOperation);

                // After creating the first condition, see if we need to create a second leaf condition
                if (prop1prop2OperationComboBox_SelectedIndex != 0 &&
                    !(string.IsNullOrEmpty(prop2TextBox_Text)))
                {
                    SearchCondition prop2Condition = SearchConditionFactory.CreateLeafCondition(
                        GetSearchProperty(prop2ComboBox_Value),
                        prop2TextBox_Text,
                        prop2ConditionOperation);

                    // Create our combined search condition AND or OR
                    if (prop1prop2OperationComboBox.SelectedIndex == 1)
                        combinedPropSearchCondition = SearchConditionFactory.CreateAndOrCondition(
                            SearchConditionType.And,
                            false, prop1Condition, prop2Condition);
                    else
                        combinedPropSearchCondition = SearchConditionFactory.CreateAndOrCondition(
                            SearchConditionType.Or,
                            false, prop1Condition, prop2Condition);
                }
                else
                    combinedPropSearchCondition = prop1Condition;
            }
            else
                return; // no search text entered

            // Get the date condition
            if (dateSelected)
            {
                SearchCondition dateCreatedCondition = SearchConditionFactory.CreateLeafCondition(
                    SystemProperties.System.DateCreated,
                    ParseDate(((ComboBoxItem)comboBoxDateCreated.SelectedItem).Tag.ToString(), DateTime.Now),
                    SearchConditionOperation.GreaterThan);

                // If we have a property based search condition, create an "AND" search condition from these 2
                if (combinedPropSearchCondition != null)
                    finalSearchCondition = SearchConditionFactory.CreateAndOrCondition(SearchConditionType.And,
                        false, combinedPropSearchCondition, dateCreatedCondition);
                else
                    finalSearchCondition = dateCreatedCondition;
            }
            else
                finalSearchCondition = combinedPropSearchCondition;


            ShellSearchFolder searchFolder = new ShellSearchFolder(finalSearchCondition, scopes.ToArray());
            
            //
            List<SearchItem> items = new List<SearchItem>();

            try
            {
                // Because we cannot pass ShellObject or IShellItem (native interface)
                // across multiple threads, creating a helper object and copying the data we need from the ShellObject
                foreach (ShellObject so in searchFolder)
                {
                    // For each of our ShellObject,
                    // create a SearchItem object
                    // We will bind these items to the ListView
                    SearchItem item = new SearchItem();
                    item.Name = so.Name;

                    // We must freeze the ImageSource before passing across threads
                    BitmapSource thumbnail = so.Thumbnail.MediumBitmapSource;
                    thumbnail.Freeze();
                    item.Thumbnail = thumbnail;

                    item.Authors = so.Properties.System.Author.Value;
                    item.Title = so.Properties.System.Title.Value;
                    item.Keywords = so.Properties.System.Keywords.Value;
                    item.Copyright = so.Properties.System.Copyright.Value;
                    item.TotalPages = so.Properties.System.Document.PageCount.Value.HasValue ? so.Properties.System.Document.PageCount.Value.Value : 0;
                    item.Rating = so.Properties.System.SimpleRating.Value.HasValue ? (int)so.Properties.System.SimpleRating.Value.Value : 0;
                    item.ParsingName = so.ParsingName;

                    items.Add(item);
                }

                // Invoke the search on the main thread

                Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(
                    delegate()
                    {
                        MainWindow.UpdateSearchItems(items);
                    }
                ));
            }
            finally
            {
                // TODO - dispose other

                finalSearchCondition.Dispose();
                finalSearchCondition = null;

                searchFolder.Dispose();
                searchFolder = null;
            }
        }
Example #3
0
        // Helper method to do the search on a background thread
        internal void DoSimpleSearch(object arg)
        {
            string text = arg as string;

            // Specify a culture for our query.
            CultureInfo cultureInfo = new CultureInfo("en-US");

            SearchCondition searchCondition = SearchConditionFactory.ParseStructuredQuery(text, cultureInfo);

            // Create a new search folder by setting our search condition and search scope
            // KnownFolders.SearchHome - This is the same scope used by Windows search
            searchFolder = new ShellSearchFolder(
                searchCondition,
                selectedScope);

            List<SearchItem> items = new List<SearchItem>();

            try
            {
                // Because we cannot pass ShellObject or IShellItem (native interface)
                // across multiple threads, creating a helper object and copying the data we need from the ShellObject
                foreach (ShellObject so in searchFolder)
                {
                    // For each of our ShellObject,
                    // create a SearchItem object
                    // We will bind these items to the ListView
                    SearchItem item = new SearchItem();
                    item.Name = so.Name;

                    // We must freeze the ImageSource before passing across threads
                    BitmapSource thumbnail = so.Thumbnail.MediumBitmapSource;
                    thumbnail.Freeze();
                    item.Thumbnail = thumbnail;

                    item.Authors = so.Properties.System.Author.Value;
                    item.Title = so.Properties.System.Title.Value;
                    item.Keywords = so.Properties.System.Keywords.Value;
                    item.Copyright = so.Properties.System.Copyright.Value;
                    item.TotalPages = so.Properties.System.Document.PageCount.Value.HasValue ? so.Properties.System.Document.PageCount.Value.Value : 0;
                    item.Rating = so.Properties.System.SimpleRating.Value.HasValue ? (int)so.Properties.System.SimpleRating.Value.Value : 0;
                    item.ParsingName = so.ParsingName;

                    items.Add(item);
                }

                // Invoke the search on the main thread

                Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(
                    delegate()
                    {
                        UpdateSearchItems(items);
                    }
                ));
            }
            catch
            {
                searchFolder.Dispose();
                searchFolder = null;
            }
        }