Changes to Login URI
[pithos-ms-client] / trunk / Pithos.Client.WPF / AppBootstrapper.cs
1 #region\r
2 /* -----------------------------------------------------------------------\r
3  * <copyright file="AppBootstrapper.cs" company="GRNet">\r
4  * \r
5  * Copyright 2011-2012 GRNET S.A. All rights reserved.\r
6  *\r
7  * Redistribution and use in source and binary forms, with or\r
8  * without modification, are permitted provided that the following\r
9  * conditions are met:\r
10  *\r
11  *   1. Redistributions of source code must retain the above\r
12  *      copyright notice, this list of conditions and the following\r
13  *      disclaimer.\r
14  *\r
15  *   2. Redistributions in binary form must reproduce the above\r
16  *      copyright notice, this list of conditions and the following\r
17  *      disclaimer in the documentation and/or other materials\r
18  *      provided with the distribution.\r
19  *\r
20  *\r
21  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS\r
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR\r
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF\r
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
32  * POSSIBILITY OF SUCH DAMAGE.\r
33  *\r
34  * The views and conclusions contained in the software and\r
35  * documentation are those of the authors and should not be\r
36  * interpreted as representing official policies, either expressed\r
37  * or implied, of GRNET S.A.\r
38  * </copyright>\r
39  * -----------------------------------------------------------------------\r
40  */\r
41 #endregion\r
42 using System.Windows;\r
43 using System.Windows.Controls;\r
44 using System.Windows.Navigation;\r
45 using Caliburn.Micro;\r
46 using Caliburn.Micro.Logging;\r
47 using Pithos.Client.WPF.Properties;\r
48 using Pithos.Core;\r
49 using Pithos.Network;\r
50 using Xceed.Wpf.Toolkit;\r
51 using log4net.Appender;\r
52 using log4net.Config;\r
53 using log4net.Filter;\r
54 using log4net.Layout;\r
55 \r
56 namespace Pithos.Client.WPF\r
57 {\r
58         using System;\r
59         using System.Collections.Generic;\r
60         using System.ComponentModel.Composition;\r
61         using System.ComponentModel.Composition.Hosting;\r
62         using System.ComponentModel.Composition.Primitives;\r
63         using System.Linq;\r
64         using Caliburn.Micro;\r
65 \r
66         public class AppBootstrapper : Bootstrapper<IShell>\r
67         {\r
68 \r
69                 CompositionContainer container;\r
70 \r
71                 public AppBootstrapper()\r
72                 {\r
73                         LogManager.GetLog = type => new log4netLogger(type);\r
74                         UpgradeSettings();\r
75                 }\r
76 \r
77                 private void UpgradeSettings()\r
78                 {\r
79                         if (Settings.Default.MustUpgrade)\r
80                         {\r
81                             Settings.Default.HashingParallelism = (byte) (Environment.ProcessorCount/2);\r
82                                 Settings.Default.Upgrade();\r
83                                 Settings.Default.MustUpgrade = true;\r
84                                 Settings.Default.Save();\r
85                         }\r
86                 }\r
87 \r
88         protected override IEnumerable<System.Reflection.Assembly> SelectAssemblies()\r
89         {\r
90             var assemblies= base.SelectAssemblies();\r
91             return assemblies.Union(new[]\r
92                 {\r
93                     typeof(PithosMonitor).Assembly, \r
94                     typeof(CloudFilesClient).Assembly,\r
95                     typeof (OFM.FileManagerViewModel).Assembly\r
96                 });\r
97         }\r
98 \r
99                 /// <summary>\r
100                 /// By default, we are configured to use MEF\r
101                 /// </summary>\r
102                 protected override void Configure() {\r
103                         var catalog = new AggregateCatalog(\r
104                                 AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()\r
105                                 );\r
106 \r
107                         /*Type[] types = { typeof(PithosMonitor), typeof(CloudFilesClient),typeof(OFM.FileManagerViewModel) };\r
108                         foreach (var type in types)\r
109                         {\r
110                                 catalog.Catalogs.Add(new AssemblyCatalog(type.Assembly));\r
111                         }\r
112 */\r
113                         container = new CompositionContainer(catalog,true);\r
114 \r
115                         var batch = new CompositionBatch();\r
116 \r
117                         batch.AddExportedValue<IWindowManager>(new WindowManager());\r
118                         batch.AddExportedValue<IEventAggregator>(new EventAggregator());\r
119                         batch.AddExportedValue(container);\r
120                         batch.AddExportedValue(catalog);\r
121 \r
122 \r
123                         container.Compose(batch);\r
124                     \r
125                         ConventionManager.AddElementConvention<MenuItem>(ItemsControl.ItemsSourceProperty, "DataContext", "Click");\r
126                         ConventionManager.AddElementConvention<IntegerUpDown>(IntegerUpDown.ValueProperty, "Value", "ValueChanged");\r
127                     ConventionManager.AddElementConvention<BusyIndicator>(BusyIndicator.IsBusyProperty, "IsBusy", "DataContextChanged");\r
128                 }\r
129 \r
130                 protected override object GetInstance(Type serviceType, string key)\r
131                 {\r
132                         string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;\r
133                         var exports = container.GetExportedValues<object>(contract);            \r
134 \r
135                         if (exports.Any())\r
136                                 return exports.First();\r
137 \r
138                         throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));\r
139                 }\r
140 \r
141                 protected override IEnumerable<object> GetAllInstances(Type serviceType)\r
142                 {\r
143                         return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));\r
144                 }\r
145 \r
146                 protected override void BuildUp(object instance)\r
147                 {\r
148                         container.SatisfyImportsOnce(instance);\r
149                 }\r
150         }\r
151 }