* Filters/FilterPackage.cs, Filters/FilterRPM.cs,
[beagle.git] / Util / Evolution.cs
blob3f408bd9b949d9be813bc9a8e242aa399eb75142
1 //
2 // Evolution.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
28 using System;
29 using System.IO;
30 using System.Collections;
32 using Mono.Unix;
34 namespace Beagle.Util {
35 // FIXME: IMAP4
36 public class Evolution
38 public static ICollection Accounts
40 get {
41 ArrayList accounts = new ArrayList ();
42 string mail_dir = Path.Combine (Environment.GetEnvironmentVariable("HOME"), Path.Combine (".evolution", "mail"));
44 if (Directory.Exists (Path.Combine (mail_dir, "local"))) {
45 MailAccount local_account = new MailAccount (Catalog.GetString ("On This Computer"), Path.Combine (mail_dir, "local"));
46 local_account.AddChildren (GetLocalChildren (local_account));
47 accounts.Add (local_account);
50 if (!Directory.Exists (Path.Combine (mail_dir, "imap"))) {
51 return accounts;
53 DirectoryInfo imap_accounts_dir = new DirectoryInfo (Path.Combine (mail_dir, "imap"));
54 foreach (DirectoryInfo imap_account_dir in imap_accounts_dir.GetDirectories ()) {
55 MailAccount imap_account = new MailAccount (imap_account_dir.Name, imap_account_dir.FullName);
56 imap_account.AddChildren (GetImapChildren (imap_account));
57 accounts.Add (imap_account);
60 return accounts;
64 private static ICollection GetLocalChildren (MailFolder folder) {
65 ArrayList children = new ArrayList ();
66 DirectoryInfo folder_dir;
68 folder_dir = new DirectoryInfo (folder.Path);
70 if (!folder_dir.Exists)
71 folder_dir = new DirectoryInfo (folder_dir.FullName + ".sbd");
73 if (!folder_dir.Exists)
74 return children;
76 foreach (FileInfo child_summary_file in folder_dir.GetFiles ("*.cmeta")) {
77 MailFolder child_folder = new MailFolder (child_summary_file.Name.Substring (0,child_summary_file.Name.Length-6),
78 child_summary_file.FullName.Substring (0, child_summary_file.FullName.Length-6));
79 child_folder.AddChildren (GetLocalChildren (child_folder));
80 children.Add (child_folder);
83 return children;
86 private static ICollection GetImapChildren (MailFolder folder) {
87 ArrayList children = new ArrayList ();
88 DirectoryInfo folder_dir;
90 folder_dir = new DirectoryInfo (Path.Combine(folder.Path,"folders"));
92 if (!folder_dir.Exists)
93 folder_dir = new DirectoryInfo (Path.Combine(folder.Path,"subfolders"));
95 if (!folder_dir.Exists)
96 return children;
98 foreach (DirectoryInfo child_folder_dir in folder_dir.GetDirectories ()) {
99 MailFolder child_folder = new MailFolder (child_folder_dir.Name, child_folder_dir.FullName);
100 child_folder.AddChildren (GetImapChildren (child_folder));
101 children.Add (child_folder);
104 return children;
108 public class MailAccount : MailFolder
110 public MailAccount (string name, string path) : base (name, path) { }
113 public class MailFolder
115 string name, path;
116 public string Name {
117 get { return name; }
118 set { name = value; }
121 public string Path {
122 get { return path; }
123 set { path = value; }
126 public MailFolder (string name, string path) {
127 this.name = name;
128 this.path = path;
131 ArrayList children = new ArrayList ();
133 public void AddChild (MailFolder child)
135 this.children.Add (child);
138 public void AddChildren (ICollection children)
140 this.children.AddRange (children);
143 public ICollection Children
145 get {
146 return children;
150 public override string ToString ()
152 return GetNameForPath (path);
155 private static string[] crap = { "/imap/", "/local/", "/imap4/", "/folders", "/subfolder", ".sbd"};
157 public static string GetNameForPath (string folder_path)
159 if (folder_path.StartsWith (System.IO.Path.Combine (Environment.GetEnvironmentVariable ("HOME"),".evolution/mail")))
160 folder_path = folder_path.Substring (System.IO.Path.Combine (Environment.GetEnvironmentVariable ("HOME"),".evolution/mail").Length);
162 if (folder_path.StartsWith ("/local/"))
163 folder_path = String.Format ("{0}/{1}", Catalog.GetString ("On This Computer"), folder_path);
165 foreach (string shit in crap)
166 folder_path = folder_path.Replace (shit, "");
168 return folder_path;