public static List<TagItem> GetTagsList(TagFilesDatabase currentDb, TagsCombinaton tagsCombination, string searchText)
        {
            if (currentDb == null) return null;

            List<TagItem> ret = new List<TagItem>();
            // Add each tag from the database
            for (int i = 0; i < currentDb.Tags.Count; i++)
            {
                // Extract the tag
                FileTag tag = currentDb.Tags[i];

                // Show the tag only if it's not in the history of tags being selected - 
                // (You should not show tag 'X' after selecting tag 'X')                                    
                if (!tagsCombination.ContainsByValue(tag, false) && tag.Value.ToLower().Contains(searchText.ToLower()))
                {
                    TagItem item = new TagItem();
                    // Set the tag item (including text of the tag with the tag's file-count                    
                    item.Text = (tag.Value == "" ? No_Tags_String : tag.ToString()) +
                        "\n  [" + currentDb.CountFilesByTag(tag).ToString() + "]";
                    item.RawFileTag = tag;

                    // Map the tag to it's group
                    int groupKeyIndex = currentDb.tagGroupMapping[i];
                    item.GroupName = currentDb.groupsNames[groupKeyIndex];
                    item.GroupKey = currentDb.groupsKeys[groupKeyIndex];

                    ret.Add(item);
                }
            }
            return ret;
        }
Exemple #2
0
        public SQL_Manager(TagFilesDatabase database)
        {            
            if (false)
            {
                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = this.connectionString;
                connection.Open();

                SqlCommand myCommand = new SqlCommand();
                myCommand.Connection = connection;
                int fileIndex = 1;
                int tagIndex = 0;
                foreach (FileWithTags file in database.Files)
                {
                    foreach (FileTag tag in file.Tags)
                    {
                        tagIndex = database.Tags.IndexOf(tag) + 1;

                        myCommand.CommandText = "INSERT INTO Files_Tags ([File ID], [Tag ID]) " +
                            "Values (" + fileIndex.ToString() + ", " + tagIndex.ToString() + ")";

                        myCommand.ExecuteNonQuery();
                    }                                        
                    fileIndex++;
                }
                connection.Close();
            }            
        }
        public BrowseTagsForm()
        {
            InitializeComponent();
            

            // Initialize all the members of the form
            this.fullDatabase = new TagFilesDatabase();
            this.currentDatabase = new TagFilesDatabase();
            this.tagsCombination = new TagsCombinaton();
            this.iconExtractor = new IconExtractor(this.imageListSmallIcons,
                this.imageListLargeIcons, 2);            

            // Subscribe to the FileSystemWatcher events
            this.fileSystemWatcherAllFiles.Renamed += new System.IO.RenamedEventHandler(fileSystemWatcher1_Renamed);
            this.fileSystemWatcherAllFiles.Deleted += new System.IO.FileSystemEventHandler(fileSystemWatcher1_Deleted);
            this.fileSystemWatcherAllFiles.Created += new System.IO.FileSystemEventHandler(fileSystemWatcher1_Created);                        

            // Subscribe to the TagsCombiationViewer events
            this.tagsCombinationViewer.RemoveRequested += new TagsCombinationViewer.RemoveHandler(tagsCombinationViewer1_RemoveRequested);
            this.tagsCombinationViewer.InverseRequested += new TagsCombinationViewer.InverseHandler(tagsCombinationViewer1_InverseRequested);

                        
            InitializeVirtualObjectListView();
            InitializeTagsListView();
            SetWindowTheme(this.listViewTags.Handle, "explorer", null);
            SetWindowTheme(this.virtualListViewFiles.Handle, "explorer", null);
        }
        public ManageTagsForm(string databaseFileName)
        {
            InitializeComponent();
            this.originalTagsList = new List<FileTag>();
            this.proposedTags = new List<FileTag>();
            
            this.DatabaseFileName = databaseFileName;

            //* In case we have a database to refernce, use its tags list for the auto-complete source
            if (this.DatabaseFileName != "")
            {
                this.database = TagFilesDatabase.DeSerialize(this.DatabaseFileName);

                this.autoCompleteSource = new AutoCompleteStringCollection();
                foreach (FileTag tag in this.database.Tags)
                {
                    this.autoCompleteSource.Add(tag.Value);
                }
                this.txtAddTag.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.txtAddTag.AutoCompleteCustomSource = this.autoCompleteSource;
                this.txtAddTag.AutoCompleteSource = AutoCompleteSource.CustomSource;
            }                       
        }
        public void UpdateItems(TagFilesDatabase currentDb, TagsCombinaton tagsCombination, string searchText)
        {
            var tagItems = TagsListWraper.GetTagsList(currentDb, tagsCombination, searchText);            
            Dictionary<string, string> groups = new Dictionary<string,string>();
            if (tagItems != null)
            for (int i = 0; i < tagItems.Count; i++)
                groups[tagItems[i].GroupKey] = tagItems[i].GroupName;

            // Create the groups list. (Skip the default group - index 0)
            this.Groups.Clear();
            foreach (var kvp in groups)
                this.Groups.Add(kvp.Key, kvp.Value);            
            
            // Create item for the listView:
            List<ListViewItem> tagsList = new List<ListViewItem>();
            ListViewItem tagItem;
            // Add each tag from the database
            int j = 0;
            foreach (var t in tagItems)
            {
                // Show the tag only if it's not in the history of tags being selected - 
                // (You should not show tag 'X' after selecting tag 'X')
                if (!tagsCombination.ContainsByValue(t.RawFileTag, false) && 
                    t.Value.ToLower().Contains(searchText.ToLower()))
                {
                    // Set the tag item (including text of the tag with the tag's file-count
                    tagItem = new ListViewItem();
                    //tagItem.Text = j++.ToString().PadLeft(9, 'a') + "\n [10]";
                    tagItem.Text = t.Text;
                    tagItem.Tag = t.RawFileTag;

                    // Set image of the tag: special image if the tag's file-count is equals the
                    // database file-count. a regular folder image if not.
                    if (currentDb.Files.Count == currentDb.CountFilesByTag(t.RawFileTag))
                        tagItem.ImageIndex = 1;
                    else
                        tagItem.ImageIndex = 0;

                    // Map the tag to it's group
                    tagItem.Group = this.Groups[t.GroupKey];
                    tagsList.Add(tagItem);
                    //this.Items.Add(tagItem);
                }
            }

            // ** Sort the tags by groups, and inside of each group
            //ListViewItem tmp = new ListViewItem();
            //for (int i = 0; i < tagsList.Count; i++)
            //{
            //    for (int j = i + 1; j < tagsList.Count; j++)
            //    {
            //        int comperator = tagsList[i].Group.Name.CompareTo(tagsList[j].Group.Name);
            //        if (comperator == 1) // item[i].Group > item[j].Group
            //        { // Swap items
            //            tmp = (ListViewItem)tagsList[i].Clone();
            //            tagsList[i] = (ListViewItem)tagsList[j].Clone();
            //            tagsList[j] = tmp;
            //        }
            //        else if (comperator == 0) // Equal groups
            //        { // Sort internally

            //            comperator = ((FileTag)tagsList[i].Tag).ToString().CompareTo(
            //                ((FileTag)tagsList[j].Tag).ToString());

            //            if (comperator == 1)
            //            {
            //                tmp = (ListViewItem)tagsList[i].Clone();
            //                tagsList[i] = (ListViewItem)tagsList[j].Clone();
            //                tagsList[j] = tmp;
            //            }
            //        }
            //    }
            //}


            // ** Add to list view
            this.BeginUpdate();
            this.Clear();
            foreach (ListViewItem item in tagsList)
               this.Items.Add(item);
            
            this.EndUpdate();        
        }
 public void Browse(List<string> paths)
 {
     this.fullDatabase = TagFilesDatabase.DeSerialize(databaseFileName);
     this.currentDatabase = this.fullDatabase.GetPartialDatabase(paths);
 }
        private void menuMain_File_Restore_Click(object sender, EventArgs e)
        {
            // Load database
            this.fullDatabase = TagFilesDatabase.DeSerialize(databaseFileName);

            SQL_Manager sql = new SQL_Manager(this.fullDatabase);
            sql.GetFilesByTag(new List<string> {"ירון", "תמר"});
            // Show the database
            this.showCurrentDatabse();
        }
 /// <summary>
 /// 
 /// </summary>
 private void showCurrentDatabse()
 {
     this.currentDatabase = this.fullDatabase.GetPartialDatabase(this.tagsCombination);
     this.updateView(this.currentDatabase, this.tagsCombination);
 }
        /// <summary>
        /// This method is in charge of updating the tags listView and the files listView in accordence
        /// with the data stored in the object 'database' and in 'tagsCombination'.
        /// </summary>
        /// <param name="database"></param>
        /// <param name="tagsCombination"></param>
        private void updateView(TagFilesDatabase database, TagsCombinaton tagsCombination)
        {
            // Update tags listview
            this.updateTagsListView(this.listViewTags, database, tagsCombination, this.txtSearchTag.Text);

            // Update files listview
            this.updateFilesListView(this.listViewFiles);            

            // Update the TagsCombinationViewer control
            this.tagsCombinationViewer.Update(tagsCombination);
        }
        /// <summary>
        /// 
        /// </summary>
        private void updateTagsListView(ListView tagsListView, TagFilesDatabase database, TagsCombinaton tagsCombination, string searchTagText)
        {
            //for (int i = 0; i < 5; i++)
            //{
            //    tagsListView1.Groups.Add("key_"+i.ToString(), "name_" + i.ToString());
            //}
            //tagsListView.BeginUpdate();
            //tagsListView.Clear();
            //for (int i = 0; i < 100; i++)
            //{
            //    ListViewItem item = new ListViewItem();
            //    item.Text = "T".PadLeft(12, 'a');
            //    item.ImageIndex = i % 2;
            //    item.Group = tagsListView1.Groups[i % 5];
            //    item.Tag = new object();
            //    this.tagsListView1.Items.Add(item);
            //}
            //tagsListView.EndUpdate();
            //return;

            this.setTagsListViewObject();

            this.tagsListView1.UpdateItems(database, tagsCombination, searchTagText);

            // Create the groups list. (Skip the default group - index 0)
            tagsListView.Groups.Clear();
            for (int i = 1; i < database.groupsNames.Count; i++)
            {
                string groupName = database.groupsNames[i];
                string groupKey = database.groupsKeys[i];
                tagsListView.Groups.Add(groupKey, groupName);
            }
            // Add the default group (with index 0) as the last group
            tagsListView.Groups.Add(database.groupsKeys[0], database.groupsNames[0]);

            // Create item for the listView:
            List<ListViewItem> tagsList = new List<ListViewItem>();
            ListViewItem tagItem;
            // Add each tag from the database
            for (int i = 0; i < database.Tags.Count; i++)
            {
                // Extract the tag
                FileTag tag = database.Tags[i];

                // Show the tag only if it's not in the history of tags being selected - 
                // (You should not show tag 'X' after selecting tag 'X')
                bool showTagCondition;
                showTagCondition =                   
                    !tagsCombination.ContainsByValue(tag, false) && 
                    tag.Value.ToLower().Contains(searchTagText.ToLower());
                if (showTagCondition)
                {
                    // Set the tag item (including text of the tag with the tag's file-count
                    tagItem = new ListViewItem();
                    tagItem.Text = (tag.Value == "" ? noTagsString : tag.ToString()) +
                        "\n  [" + database.CountFilesByTag(tag).ToString() + "]";
                    tagItem.Tag = tag;

                    // Set image of the tag: special image if the tag's file-count is equals the
                    // database file-count. a regular folder image if not.
                    if (database.Files.Count == database.CountFilesByTag(tag))
                        tagItem.ImageIndex = 1;
                    else
                        tagItem.ImageIndex = 0;

                    // Map the tag to it's group
                    int groupKeyIndex = database.tagGroupMapping[i];
                    string groupKey = database.groupsKeys[groupKeyIndex];
                    tagItem.Group = tagsListView.Groups[groupKey];
                    tagsList.Add(tagItem);
                }
            }

            // ** Sort the tags by groups, and inside of each group
            ListViewItem tmp = new ListViewItem();
            for (int i = 0; i < tagsList.Count; i++)
            {
                for (int j = i + 1; j < tagsList.Count; j++)
                {
                    int comperator = tagsList[i].Group.Name.CompareTo(tagsList[j].Group.Name);
                    if (comperator == 1) // item[i].Group > item[j].Group
                    { // Swap items
                        tmp = (ListViewItem)tagsList[i].Clone();
                        tagsList[i] = (ListViewItem)tagsList[j].Clone();
                        tagsList[j] = tmp;
                    }
                    else if (comperator == 0) // Equal groups
                    { // Sort internally

                        comperator = ((FileTag)tagsList[i].Tag).ToString().CompareTo(
                            ((FileTag)tagsList[j].Tag).ToString());

                        if (comperator == 1)
                        {
                            tmp = (ListViewItem)tagsList[i].Clone();
                            tagsList[i] = (ListViewItem)tagsList[j].Clone();
                            tagsList[j] = tmp;
                        }
                    }
                }
            }


            // ** Add to list view
            tagsListView.BeginUpdate();
            tagsListView.Clear();
            foreach (ListViewItem item in tagsList)
                tagsListView.Items.Add(item);
            
            tagsListView.EndUpdate();
        }
        public TagFilesDatabase GetReversePartialDatabase(List<FileTag> matchTags)
        {
            if (matchTags == null)
                throw new ArgumentNullException();
            if (matchTags.Count == 0)
                throw new ArgumentException();

            // The returned value
            TagFilesDatabase ret = new TagFilesDatabase();

            // Check each file if it needs to be added
            foreach (FileWithTags file in this.files)
            {
                bool skippFile = false;

                // Search for each tag in the file
                foreach (FileTag tag in matchTags)
                {
                    // If the tag exists skipp the file
                    if (file.Tags.Exists(tag.Compare))
                    {
                        skippFile = true;
                        break;
                    }
                }

                if (!skippFile)
                    ret.Files.Add(file);
            }

            // Create all the groups
            for (int i = 0; i < this.groupsKeys.Count; i++)
            {
                ret.CreateGroup(this.groupsNames[i], this.groupsKeys[i]);
            }
            // Add all the relevant mappings
            for (int i = 0; i < this.tags.Count; i++)
            {
                int index = ret.tags.FindIndex(this.tags[i].Compare);
                if (index >= 0)
                    ret.tagGroupMapping[index] = this.tagGroupMapping[i];
            }
            // Subscribe to the parent's events
            ret.FileAdded = this.FileAdded;
            ret.FileRemoved = this.FileRemoved;
            ret.TagAdded = this.TagAdded;
            ret.TagRemoved = this.TagRemoved;

            return ret;
        }
        public TagFilesDatabase GetReversePartialDatabase(FileTag matchTag)
        {
            TagFilesDatabase ret = new TagFilesDatabase();

            List<FileTag> list = new List<FileTag>();
            list.Add(matchTag);

            ret = this.GetReversePartialDatabase(list);
            return ret;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public TagFilesDatabase GetPartialDatabase(List<string> paths)
        {
            if (paths == null)
                throw new ArgumentNullException();
            if (paths.Count == 0)
                throw new ArgumentNullException();

            // The returned value
            TagFilesDatabase ret = new TagFilesDatabase();

            // Check each file if it needs to be added
            foreach (FileWithTags file in this.files)
            {
                // Search for each directory
                foreach (string path in paths)
                {
                    if (file.FileName.ToLower().StartsWith(path.ToLower()))
                    {
                        ret.Files.Add(file);
                        break;
                    }
                }
            }

            // Create all the groups
            for (int i = 0; i < this.groupsKeys.Count; i++)
            {
                ret.CreateGroup(this.groupsNames[i], this.groupsKeys[i]);
            }
            // Add all the relevant mappings
            for (int i = 0; i < this.tags.Count; i++)
            {
                int index = ret.tags.FindIndex(this.tags[i].Compare);
                if (index >= 0)
                    ret.tagGroupMapping[index] = this.tagGroupMapping[i];
            }
            // Subscribe to the parent's events
            ret.FileAdded = this.FileAdded;
            ret.FileRemoved = this.FileRemoved;
            ret.TagAdded = this.TagAdded;
            ret.TagRemoved = this.TagRemoved;

            return ret;
        }
        /// <summary>
        /// Retrives a partial database that contains all the files that have a tag in the list of tags
        /// </summary>
        /// <param name="matchTag">List of tags to match with the current database of files</param>
        /// <returns>The partial database</returns>        
        public TagFilesDatabase GetPartialDatabase(List<FileTag> matchTags)
        {
            if (matchTags == null)
                throw new ArgumentNullException();
            if (matchTags.Count == 0)
                throw new ArgumentException();

            // The returned value
            TagFilesDatabase ret = new TagFilesDatabase();

            // Check each file if it needs to be added
            foreach (FileWithTags file in this.files)
            {
                // Search for each tag
                foreach (FileTag tag in matchTags)
                {
                    if (!tag.Inverse)
                    {
                        if ((tag.Value == FileTag.Empty.Value && file.Tags.Count == 0) ||
                            file.Tags.Exists(tag.Compare))
                        {
                            ret.Files.Add(file);
                            break;
                        }
                    }
                    else // Tag is inversed
                    {
                        if ((tag.Value == FileTag.Empty.Value && file.Tags.Count != 0) ||
                            !file.Tags.Exists(tag.Compare))
                        {
                            ret.Files.Add(file);
                            break;
                        }
                    }
                }
            }

            // Create all the groups
            for (int i = 0; i < this.groupsKeys.Count; i++)
            {
                ret.CreateGroup(this.groupsNames[i], this.groupsKeys[i]);
            }
            // Add all the relevant mappings
            for (int i = 0; i < this.tags.Count; i++)
            {
                int index = ret.tags.FindIndex(this.tags[i].Compare);
                if (index >= 0)
                    ret.tagGroupMapping[index] = this.tagGroupMapping[i];
            }
            // Subscribe to the parent's events
            ret.FileAdded = this.FileAdded;
            ret.FileRemoved = this.FileRemoved;
            ret.TagAdded = this.TagAdded;
            ret.TagRemoved = this.TagRemoved;

            return ret;
        }        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static TagFilesDatabase DeSerialize(string fileName)
        {
            TagFilesDatabase ret = new TagFilesDatabase();
            
            if (File.Exists(fileName))
            {
                string line = "";

                FileStream fileStream = new FileStream(fileName, FileMode.Open);
                StreamReader reader = new StreamReader(fileStream);

                while (!reader.EndOfStream)
                {
                    line = reader.ReadLine();

                    // Read the files
                    if (line == fileBeginString)
                    {
                        // Read the file name
                        FileWithTags file = new FileWithTags();
                        file.FileName = reader.ReadLine();

                        // Read the tags
                        line = reader.ReadLine();
                        while (line != fileEndString)
                        {
                            FileTag tag = new FileTag();
                            tag.Value = line;
                            file.Tags.Add(tag);

                            line = reader.ReadLine();
                        }
                        ret.Files.Add(file);
                    }
                    // Read the groups
                    else if (line == groupsBeginString)
                    {
                        line = reader.ReadLine();
                        while (line != groupsEndString)
                        {
                            string name = line;
                            string key = reader.ReadLine();
                            ret.CreateGroup(name, key);
                            line = reader.ReadLine();
                        }
                    }
                    // Read the mapping between tags and groups
                    else if (line == mappingBeginString)
                    {                        
                        line = reader.ReadLine();                        
                        int index = 0;
                        while (line != mappingEndString)
                        {
                            // read the tag line
                            FileTag tag = new FileTag(line);
                            // read the group index line
                            int groupIndex = int.Parse(reader.ReadLine());

                            // find the tag in the list
                            index = ret.tags.FindIndex(tag.Compare);
                            if (index >= 0)
                            {
                                ret.tagGroupMapping[index] = groupIndex;
                            }                                                      
                            line = reader.ReadLine();
                        }
                    }
                }

                reader.Dispose();
                fileStream.Close();
            }

            return ret;
        }