Dont add null mimetypes. Fixes bgo# 337431. The patch hasnt been officially accepted...
[beagle.git] / search / GroupView.cs
blobcd1f470df39fed504a47cffa895026f4b02d0277
1 using System;
2 using System.Collections;
4 using Mono.Posix;
5 using Gtk;
6 using Beagle;
7 using Search.Tiles;
9 namespace Search {
11 public delegate void TileHandler (Tiles.Tile tile);
13 public enum MatchType {
14 None,
15 NoneInScope,
16 Matched
19 public class GroupView : VBox {
21 public event TileHandler TileSelected;
22 private Gtk.SizeGroup tileSizeGroup;
23 private Hashtable categories;
24 private Gtk.Widget selection;
26 public GroupView () : base (false, 0)
28 categories = new Hashtable ();
29 tileSizeGroup = new Gtk.SizeGroup (Gtk.SizeGroupMode.Both);
30 Category box = null;
32 foreach (Tiles.TileGroupInfo info in Tiles.Utils.GroupInfo) {
34 if (info.Group == Tiles.TileGroup.Conversations)
35 box = new ConversationCategory (info.Name, info.Rows);
36 else
37 box = new TileCategory (info.Name, info.Rows, tileSizeGroup);
39 PackStart (box, false, false, 0);
40 box.NoShowAll = true;
41 categories [info.Group] = box;
44 // FIXME: Add the Best match category
47 public void AddHit (Tiles.Tile tile)
49 tile.Show ();
50 tile.Selected += OnTileSelected;
52 Category box = (Category)categories [tile.Group];
53 box.Add (tile);
55 if (GroupInScope (tile.Group))
56 box.Show ();
59 public void SubtractHit (Uri uri)
61 foreach (Category box in categories.Values) {
62 foreach (Tile tile in box.AllTiles) {
63 if (tile.Hit.Uri.Equals (uri)) {
64 if (tile.State == StateType.Selected)
65 OnTileSelected (null, EventArgs.Empty);
66 box.Remove (tile);
67 return;
73 public void Finished (bool grabFocus)
75 Category first = null;
76 bool others = false;
78 foreach (Category category in categories.Values) {
79 if (category.Visible) {
80 if (first == null)
81 first = category;
82 else {
83 others = true;
84 break;
89 if (first != null)
90 first.Select (grabFocus, !others);
93 public MatchType MatchState {
94 get {
95 bool hiddenMatches = false;
96 foreach (Category category in categories.Values) {
97 if (category.Visible)
98 return MatchType.Matched;
99 else if (!category.Empty)
100 hiddenMatches = true;
103 return hiddenMatches ? MatchType.NoneInScope : MatchType.None;
107 private void OnTileSelected (object tile, EventArgs args)
109 if (tile == selection)
110 return;
112 if (TileSelected != null)
113 TileSelected ((Tiles.Tile)tile);
115 if (selection != null)
116 selection.State = StateType.Normal;
117 selection = (Gtk.Widget)tile;
118 if (selection != null)
119 selection.State = StateType.Selected;
122 private ScopeType scope;
123 public ScopeType Scope {
124 set {
125 scope = value;
126 foreach (TileGroup group in categories.Keys) {
127 Category category = (Category)categories[group];
128 if (!GroupInScope (group))
129 category.Hide ();
130 else if (!category.Empty)
131 category.Show ();
136 private bool GroupInScope (TileGroup group)
138 switch (scope) {
139 case ScopeType.Everywhere:
140 return true;
142 case ScopeType.Applications:
143 return group == TileGroup.Application;
145 case ScopeType.Calendar:
146 return group == TileGroup.Calendar;
148 case ScopeType.Contacts:
149 return group == TileGroup.Contact;
151 case ScopeType.Documents:
152 return group == TileGroup.Documents;
154 case ScopeType.Conversations:
155 return group == TileGroup.Conversations;
157 case ScopeType.Images:
158 return group == TileGroup.Image;
160 case ScopeType.Media:
161 return group == TileGroup.Audio || group == TileGroup.Video;
164 return false;
167 public SortType Sort {
168 set {
169 foreach (Category category in categories.Values)
170 category.Sort = value;
174 public void Clear ()
176 foreach (Category box in categories.Values) {
177 box.Clear ();
178 box.Hide ();