Changeset 11911


Ignore:
Timestamp:
Aug 5, 2011, 5:00:07 PM (13 years ago)
Author:
bayral
Message:

WPF inprovement

Location:
extensions/PiwigoLib
Files:
6 added
16 edited

Legend:

Unmodified
Added
Removed
  • extensions/PiwigoLib/PiwigoLib/DTO/PwgSession.cs

    r11850 r11911  
    77    public class PwgSession
    88    {
    9         public Boolean IsConnected {
    10             get { return this.Status != PwgSessionStatusEnum.Guest; }
     9        private Boolean _isConnected = false;
     10        public Boolean IsConnected
     11        {
     12            get { return this._isConnected; }
     13            internal set { _isConnected = value; } //Is connected is set by the session service when connecting is done
    1114        }
    1215 
  • extensions/PiwigoLib/PiwigoLib/Service/PwgSessionService.cs

    r11850 r11911  
    4444                    {
    4545                        ConvertProxyResponseToDTO(response, ref PwgSession);
     46                        PwgSession.IsConnected = true;
    4647                    }
    4748                } catch (PwgProxyException ex)
  • extensions/PiwigoLib/PiwigoWpf/App.xaml

    r11905 r11911  
    66             Exit="onAppExit">
    77    <Application.Resources>
    8         <ObjectDataProvider x:Name="ImagesODP" x:Key="Images" ObjectType="{x:Type piwigo:PwgImageListWPF}" />
     8        <ObjectDataProvider x:Name="ImagesODP" x:Key="Images" ObjectType="{x:Type piwigo:PwgImageListWPF}"/> <!--IsAsynchronous="True"-->
    99        <ObjectDataProvider x:Name="SessionODP" x:Key="Session" ObjectType="{x:Type piwigo:PwgSessionWPF}" />
    10         <ObjectDataProvider x:Name="CategoriesODP" x:Key="Categories" ObjectType="{x:Type piwigo:PwgCategoryListWPF}" />
    11        
     10        <ObjectDataProvider x:Name="CategoriesODP" x:Key="Categories" ObjectType="{x:Type piwigo:PwgCategoryListWPF}"/> <!--IsAsynchronous="True"-->
     11
    1212        <ResourceDictionary  x:Key="VisualStyleApp" Source="VisualStyleApp.xaml"/>
    1313    </Application.Resources>
  • extensions/PiwigoLib/PiwigoWpf/App.xaml.cs

    r7160 r11911  
    1717            PwgServiceProvider.Instance.PwgSetupService.Setup( "Com.Piwigo.Wpf.PiwigoWPF", null, null);
    1818
     19             // Model object was created in the app.xaml, so we have to link to them
    1920            PwgModelManager.Instance.Session = (PwgSessionWPF)(this.Resources["Session"] as ObjectDataProvider).Data;
    2021            PwgModelManager.Instance.CategoryList = (PwgCategoryListWPF)(this.Resources["Categories"] as ObjectDataProvider).Data;
  • extensions/PiwigoLib/PiwigoWpf/Command/PwgCmdConnect.cs

    r11905 r11911  
    1919
    2020                PwgServiceProvider.Instance.PwgSetupService.Setup(null, null, uriServer);
    21                 PwgSession sess = PwgServiceProvider.Instance.PwgSessionService.GetPwgSession();
    22                 sess = PwgServiceProvider.Instance.PwgSessionService.Login( PwgModelManager.Instance.Session.UserName,
    23                                                                             PwgModelManager.Instance.Session.Password);
     21                PwgSessionWPF sesWPF = PwgModelManager.Instance.Session;
     22               
     23                PwgSession sess = null;
     24                if (String.IsNullOrWhiteSpace(sesWPF.UserName) && String.IsNullOrWhiteSpace(sesWPF.Password))
     25                {
     26                    sess = PwgServiceProvider.Instance.PwgSessionService.GetPwgSession();
     27                }
     28                else
     29                {
     30                    sess = PwgServiceProvider.Instance.PwgSessionService.Login(PwgModelManager.Instance.Session.UserName,
     31                                                                               PwgModelManager.Instance.Session.Password);
     32                }
    2433
    25                 PwgSessionWPF sesWPF = PwgModelManager.Instance.Session;
    2634                PwgSessionWPFHelper.AddPwgSessionInfoToPwgSessionWPF(sess, ref sesWPF);
    2735
     
    3240
    3341                ImageCacheManager.Instance.SetCurrentServer(uriServer.AbsoluteUri);
     42
    3443                IsRunning = false;
    3544            }
  • extensions/PiwigoLib/PiwigoWpf/Command/PwgCmdGetImageforCategory.cs

    r11905 r11911  
    2424                {
    2525                    catParam = (PwgCategoryWPF)parameter;
     26                    ImageCacheManager.Instance.SetCurrentCategory(catParam.Id.ToString());
    2627
    2728                    List<PwgImage> lstImg = PwgServiceProvider.Instance.PwgCategoriesService.GetListOfImagesFormCategory(catParam.Id, false, null, null, null, null, null, null, null, null, null, null, null, null, null, true, ref aPage, ref aPerPage, ref  acount);
  • extensions/PiwigoLib/PiwigoWpf/Command/PwgCmdProvider.cs

    r7160 r11911  
    99    public static class PwgCmdProvider
    1010    {
    11         public static ICommand _pwgCmdConnect;
    12         public static ICommand PwgCmdConnect
     11        public static PwgCmdBase GetByType<T>()
     12        {
     13            PwgCmdBase objICom;
     14
     15            switch (typeof(T).Name)
     16            {
     17                case "PwgCmdConnect":
     18                    objICom = PwgCmdConnect;
     19                    break;
     20                case "PwgCmdDisconnect":
     21                    objICom = PwgCmdDisconnect;
     22                    break;
     23                case "PwgCmdGetImageforCategory":
     24                    objICom = PwgCmdGetImageforCategory;
     25                    break;
     26                default:
     27                    objICom = null;
     28                    break;
     29            }
     30
     31            return objICom;
     32        }
     33
     34        public static PwgCmdBase _pwgCmdConnect;
     35        public static PwgCmdBase PwgCmdConnect
    1336        {
    1437            get
     
    2144            }
    2245        }
     46
     47        public static PwgCmdBase _pwgCmdDisconnect;
     48        public static PwgCmdBase PwgCmdDisconnect
     49        {
     50            get
     51            {
     52                if (_pwgCmdDisconnect == null)
     53                {
     54                    _pwgCmdDisconnect = new PwgCmdDisconnect();
     55                }
     56                return _pwgCmdDisconnect;
     57            }
     58        }
     59
     60        public static PwgCmdBase _pwgCmdGetImageforCategory;
     61        public static PwgCmdBase PwgCmdGetImageforCategory
     62        {
     63            get
     64            {
     65                if (_pwgCmdGetImageforCategory == null)
     66                {
     67                    _pwgCmdGetImageforCategory = new PwgCmdGetImageforCategory();
     68                }
     69                return _pwgCmdGetImageforCategory;
     70            }
     71        }
    2372    }
    2473}
  • extensions/PiwigoLib/PiwigoWpf/DTO/Helper/PwgCategoryListWPFHelper.cs

    r7160 r11911  
    3030            if (aPwgCategoryList != null)
    3131            {
    32             foreach (PwgCategory pwgCat in aPwgCategoryList)
    33             {
    34                 AddPwgCategoryToList(pwgCat, ref aPwgCategoryListWPF);
    35             }
     32                if (aPwgCategoryListWPF == null)
     33                {
     34                    aPwgCategoryListWPF = new PwgCategoryListWPF();
     35                }
     36                foreach (PwgCategory pwgCat in aPwgCategoryList)
     37                {
     38                    AddPwgCategoryToList(pwgCat, ref aPwgCategoryListWPF);
     39                }
    3640            }
    3741        }
  • extensions/PiwigoLib/PiwigoWpf/DTO/Helper/PwgSessionWPFHelper.cs

    r7160 r11911  
    1717                    aPwgSessionWPF = new PwgSessionWPF();
    1818                }
    19                 aPwgSession.UserName = aPwgSessionWPF.UserName;
    20                 aPwgSession.Status = aPwgSessionWPF.Status;
    21                 aPwgSession.Template = aPwgSessionWPF.Template;
    22                 aPwgSession.Theme = aPwgSessionWPF.Theme;
    23                 aPwgSession.CharSet = aPwgSessionWPF.CharSet;
     19                aPwgSessionWPF.IsConnected = aPwgSession.IsConnected;
     20                aPwgSessionWPF.UserName = aPwgSession.UserName;
     21                aPwgSessionWPF.Status = aPwgSession.Status;
     22                aPwgSessionWPF.Template = aPwgSession.Template;
     23                aPwgSessionWPF.Theme = aPwgSession.Theme;
     24                aPwgSessionWPF.CharSet = aPwgSession.CharSet;
    2425            }
    2526        }
  • extensions/PiwigoLib/PiwigoWpf/DTO/PwgCategoryListWPF.cs

    r7160 r11911  
    77namespace Com.Piwigo.Wpf.DTO
    88{
    9     public class PwgCategoryListWPF : ObservableCollection<PwgCategoryWPF>
     9    public class PwgCategoryListWPF : AsyncObservableCollection<PwgCategoryWPF>
    1010    {
    1111    }
  • extensions/PiwigoLib/PiwigoWpf/DTO/PwgImageListWPF.cs

    r7160 r11911  
    77namespace Com.Piwigo.Wpf.DTO
    88{
    9     public class PwgImageListWPF : ObservableCollection<PwgImageWPF>
     9    public class PwgImageListWPF : AsyncObservableCollection<PwgImageWPF>
    1010    {
    1111    }
  • extensions/PiwigoLib/PiwigoWpf/DTO/PwgSessionWPF.cs

    r7150 r11911  
    1111    public class PwgSessionWPF:  INotifyPropertyChanged
    1212    {
     13        private Boolean _isConnected;
    1314        public Boolean IsConnected {
    14             get { return this.Status != PwgSessionStatusEnum.Guest; }
     15            get { return _isConnected; }
     16            set { _isConnected = value; OnPropertyChanged("IsConnected"); }
    1517        }
    1618
     
    2527        public String Password
    2628        {
    27             set { _password = value; OnPropertyChanged("Password"); }
     29            set { _password = value; OnPropertyChanged("Password");}
    2830            get { return _password; }
    2931        }
     
    3234        public String ServeurName
    3335        {
    34             set { _serverName = value; OnPropertyChanged("ServeurName"); }
     36            set { _serverName = value; OnPropertyChanged("ServeurName");}
    3537            get { return _serverName; }
    3638        }
     
    4648        public PwgSessionStatusEnum Status
    4749        {
    48             set { _status = value; OnPropertyChanged("Status"); OnPropertyChanged("IsConnected"); }
     50            set { _status = value; OnPropertyChanged("Status"); }
    4951            get { return _status; }
    5052        }
  • extensions/PiwigoLib/PiwigoWpf/Helper/AppHelper.cs

    r11904 r11911  
    5252        }
    5353
    54         static public void ExecuteCommand<T>(object parameter) where T:  PwgCmdBase, new()
     54        static public void ExecuteCommand<T>(object parameter) where T:  PwgCmdBase
    5555        {
    56             T cmd = new T();
     56            PwgCmdBase cmd = PwgCmdProvider.GetByType<T>();
    5757            if (cmd.CanExecute(parameter))
    5858            {
  • extensions/PiwigoLib/PiwigoWpf/MainWindow.xaml

    r11904 r11911  
    33        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    44        xmlns:pwg="clr-namespace:Com.Piwigo.Wpf"
     5        xmlns:pwgui="clr-namespace:Com.Piwigo.Wpf.UI"
    56        xmlns:pwgdto="clr-namespace:Com.Piwigo.Wpf.DTO"
    67        xmlns:pwgcmd="clr-namespace:Com.Piwigo.Wpf.Command"
     
    2021                <Image ToolTip="{Binding File}" >
    2122                    <Image.Source>
    22                         <PriorityBinding FallbackValue="pack://application:,,,/Pictures/Globe.png">
     23                        <PriorityBinding>
    2324                            <Binding IsAsync="True" Converter="{x:Static pwgsrv:ImageCacheManager.ImageUrlCachedConverter}"/>
    24                            
     25                            <Binding Source="pack://application:,,,/Pictures/Globe.png" />
    2526                    </PriorityBinding>
    2627                    </Image.Source>
     
    204205                <TextBox Height="23" Name="BbUsr" Width="120" Text="{Binding Path=UserName, Mode=TwoWay}" />
    205206                <Label Content="Password" Name="LblPwd" />
    206                 <TextBox Height="23" Name="TbPwd" Width="120" Text="{Binding Path=Password, Mode=TwoWay}"/>
     207                <PasswordBox Height="23" Name="TbPwd" Width="120" PasswordChar="*"
     208                             pwgui:PasswordHelper.BindPassword="true" pwgui:PasswordHelper.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    207209                <Label Content="Url" Name="LblUrl" />
    208210                <TextBox Height="23" Name="TbUrl" Width="300" Text="{Binding Path=ServeurName, Mode=TwoWay}"/>
    209                 <CheckBox  VerticalAlignment="Center" IsChecked="{Binding Path=SaveSetting, Mode=TwoWay}">Save settings</CheckBox>
    210                 <Button Margin="2" Command="{x:Static pwgcmd:PwgCmdProvider.PwgCmdConnect}">Go</Button>
     211                <CheckBox Margin="2" VerticalAlignment="Center" IsChecked="{Binding Path=SaveSetting, Mode=TwoWay}">Save settings</CheckBox>
     212                <Button Margin="2" Command="{x:Static pwgcmd:PwgCmdProvider.PwgCmdConnect}" Visibility="{pwgui:SwitchBinding IsConnected, Hidden, Visible}">Conect</Button>
     213                <Button Margin="2" Command="{x:Static pwgcmd:PwgCmdProvider.PwgCmdDisconnect}" Visibility="{pwgui:SwitchBinding IsConnected, Visible, Hidden}">Disconect</Button>
    211214            </StackPanel>
    212215           <ProgressBar IsIndeterminate="True" />         
  • extensions/PiwigoLib/PiwigoWpf/PiwigoWpf.csproj

    r11904 r11911  
    7777      <SubType>Designer</SubType>
    7878    </ApplicationDefinition>
     79    <Compile Include="Command\PwgCmdDisconnect.cs" />
    7980    <Compile Include="Command\PwgCmdGetImageforCategory.cs" />
    8081    <Compile Include="Command\PwgCmdProvider.cs" />
     
    8889    <Compile Include="DTO\PwgModelManager.cs" />
    8990    <Compile Include="Helper\AppHelper.cs" />
     91    <Compile Include="DTO\AsyncObservableCollection.cs" />
     92    <Compile Include="UI\SwitchBindingExtension.cs" />
     93    <Compile Include="UI\PasswordHelper.cs" />
     94    <Compile Include="Helper\XMLHelper.cs" />
    9095    <Compile Include="Helper\ImageUrlCachedConverter.cs" />
    9196    <Compile Include="Service\ImageCacheManager.cs" />
  • extensions/PiwigoLib/PiwigoWpf/Service/ImageCacheManager.cs

    r11904 r11911  
    1414    public class ImageCacheManager
    1515    {
     16        #region WPF Converter
     17        static readonly ImageUrlCachedConverter _imageUrlCachedConverter = new ImageUrlCachedConverter();
     18
     19        public static ImageUrlCachedConverter ImageUrlCachedConverter
     20        {
     21            get
     22            {
     23                return _imageUrlCachedConverter;
     24            }
     25        }
     26
     27        public static String GetDefaultImage()
     28        {
     29            return ("pack://application:,,,/Pictures/Globe.png");
     30        }
     31
     32        #endregion WPF Converter
     33
     34        #region Singleton
    1635        static readonly ImageCacheManagerBase _instance = new ImageCacheManagerBase();
    17 
    18         static readonly ImageUrlCachedConverter _imageUrlCachedConverter = new ImageUrlCachedConverter();
    1936
    2037        // Explicit static constructor to tell C# compiler
     
    3148            }
    3249        }
    33 
    34         public static ImageUrlCachedConverter ImageUrlCachedConverter
    35         {
    36             get
    37             {
    38                 return _imageUrlCachedConverter;
    39             }
    40         }
    41         public class ImageCacheManagerBase
    42         {
     50        #endregion Singleton
     51
     52        #region singleton base class
     53        public class ImageCacheManagerBase : IDisposable
     54        {
     55            #region DTO
    4356            // local DTO
    44             private class ImageDictionnary : SortedDictionary<Int32, String> { };
    45             private class ServerDictionnary : SortedDictionary<String, ImageDictionnary> { };
    46 
    47             private Object _lock = new Object();
    48            
     57            internal class CategoryDictionnaryItem
     58            {
     59                internal DirectoryInfo BasePath;
     60                internal String CategorieId;
     61            };
     62
     63            internal class CategoryDictionnary : SortedDictionary<String, CategoryDictionnaryItem> { };
     64
     65            internal class ServerDictionnaryItem
     66            {
     67                internal DirectoryInfo BasePath;
     68                internal CategoryDictionnary Categories;
     69                internal Uri ServeurUrl;
     70            };
     71
     72            internal class ServerDictionnary : SortedDictionary<String, ServerDictionnaryItem> { };
     73
     74            internal static DirectoryInfo AppCachedDataDirectory = new DirectoryInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"PiwigoWpf", "cache"));
     75
     76            internal Object _lock = new Object();
     77            internal ServerDictionnary _aServerDictionnary;
     78            internal ServerDictionnaryItem _currentServer;
     79            internal CategoryDictionnaryItem _currentCategory;
     80
     81            #endregion DTO
     82
     83            #region local helper
    4984            // local helper
    5085            static private String BuildServerDirCache(String serveurUri)
    5186            {
    52                 String AppCachedDataDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"cache");
    53                 String ServerDirCache = Path.Combine(AppCachedDataDirectory, serveurUri.Replace("http://", "_").Replace("/", "_"));
     87                String ServerDirCache = Path.Combine(AppCachedDataDirectory.FullName, serveurUri.Replace("http://", "_").Replace("/", "_"));
     88                System.Diagnostics.Debug.Print(ServerDirCache);
    5489
    5590                return ServerDirCache;
    5691            }
    5792           
    58            
    59 
    60             private ServerDictionnary _aServerDictionnary = new ServerDictionnary();
    61             private String _currentServer;
    62             private ImageDictionnary _currentImageDictionnary = new ImageDictionnary();
     93            static private String BuildCategoryDirCache(ServerDictionnaryItem server, String CatgeoryId)
     94            {
     95                String CategoryDirCache = String.Empty;
     96                if (server != null)
     97                {
     98                    CategoryDirCache = Path.Combine(server.BasePath.FullName, CatgeoryId.Replace("/", "_"));
     99                }
     100                else
     101                {
     102                    throw new ApplicationException ("Current server not set before using category");
     103                }
     104
     105                return CategoryDirCache;
     106            }
     107
     108            static private ServerDictionnary BuildServerDictionnaryFromCache()
     109            {
     110                ServerDictionnary retLstServerDictionnary = new ServerDictionnary();
     111
     112                if (AppCachedDataDirectory.Exists)
     113                {
     114                    foreach (DirectoryInfo Dir in AppCachedDataDirectory.GetDirectories())
     115                    {
     116                        FileInfo xmlData = new FileInfo(Path.Combine(Dir.FullName, "serverdictionnaryitem.xml"));
     117                        if (xmlData.Exists)
     118                        {
     119                            ServerDictionnaryItem item = XMLHelper<ServerDictionnaryItem>.DeserializeObjectFromFile(xmlData);
     120                            retLstServerDictionnary.Add(item.ServeurUrl.ToString(), item);
     121                        }
     122                    }
     123                }
     124
     125                return retLstServerDictionnary;
     126            }
     127
     128            static void WriteServerDictionnaryToDisk(ServerDictionnary dcServer)
     129            {
     130                foreach (ServerDictionnaryItem Item in dcServer.Values)
     131                {
     132                    FileInfo xmlData = new FileInfo(Path.Combine(Item.BasePath.FullName, "serverdictionnaryitem.xml"));
     133                    XMLHelper<ServerDictionnaryItem>.SerializeToXMLFile(Item, xmlData);
     134                }
     135            }
     136            #endregion local helper
     137
     138            public void UnsetCurrentServer()
     139            {
     140                _currentCategory = null;
     141                _currentServer = null;
     142            }
    63143
    64144            public void SetCurrentServer(String serveurUri)
     
    67147                lock (_lock)
    68148                {
     149                    // load data from disk on first access
     150                    if (_aServerDictionnary == null)
     151                    {
     152                        _aServerDictionnary = BuildServerDictionnaryFromCache();
     153                    }
     154
    69155                    if (_aServerDictionnary.ContainsKey(serveurUri))
    70156                    {
    71                         _currentServer = serveurUri;
     157                        _currentServer = _aServerDictionnary[serveurUri];
    72158                    }
    73159                    else
    74160                    {
    75                         ImageDictionnary newImageDictionnary = new ImageDictionnary();
    76                         _aServerDictionnary.Add(serveurUri, newImageDictionnary);
    77                         _currentServer = serveurUri;
    78                     }
    79                 }
    80                 if (!Directory.Exists(currentserverDir))
    81                 {
    82                     //Create it
    83                     Directory.CreateDirectory(currentserverDir);
     161                        ServerDictionnaryItem server = new ServerDictionnaryItem();
     162                        server.ServeurUrl = new Uri(serveurUri);
     163                        server.BasePath = new DirectoryInfo(currentserverDir);
     164                        server.Categories = new CategoryDictionnary();
     165                        _aServerDictionnary.Add(serveurUri, server);
     166                        _currentServer = server;
     167                    }
     168
     169                    if (!Directory.Exists(currentserverDir))
     170                    {
     171                        //Create it
     172                        Directory.CreateDirectory(currentserverDir);
     173                        _currentServer.Categories.Clear();
     174                    }
     175                }
     176            }
     177
     178            public void SetCurrentCategory(String CategorieId)
     179            {
     180                if (_currentServer != null)
     181                {
     182                    lock (_lock)
     183                    {
     184                        String currentsCategoryDir = BuildCategoryDirCache(_currentServer, CategorieId);
     185                        if (_currentServer.Categories.ContainsKey(CategorieId))
     186                        {
     187                            _currentCategory = _currentServer.Categories[CategorieId];
     188                        }
     189                        else
     190                        {
     191                            CategoryDictionnaryItem category = new CategoryDictionnaryItem();
     192                            category.BasePath = new DirectoryInfo(currentsCategoryDir);
     193                            category.CategorieId = CategorieId;
     194                            _currentServer.Categories.Add(CategorieId, category);
     195                            _currentCategory = category;
     196                        }
     197
     198                        if (!Directory.Exists(currentsCategoryDir))
     199                        {
     200                            //Create it
     201                            Directory.CreateDirectory(currentsCategoryDir);
     202                        }
     203                    }
     204                }
     205                else
     206                {
     207                    throw new ApplicationException("Current server not set before using category");
    84208                }
    85209            }
     
    87211            public String GetImageFilename(PwgImageWPF aImageWPF)
    88212                {
     213                    String retValue = String.Empty;
    89214                    if (aImageWPF == null)
    90215                    {
    91                         return String.Empty;
    92                     }
    93 
    94                     String retValue;
    95                     String currentserverDir = BuildServerDirCache(_currentServer);
    96                     String localFile = Path.Combine(currentserverDir, String.Format("{0}-{1}", aImageWPF.Id, aImageWPF.File));
    97 
    98                     if (_aServerDictionnary[_currentServer].ContainsKey(aImageWPF.Id))
    99                     {
    100                         retValue = _aServerDictionnary[_currentServer][aImageWPF.Id];
     216                        return retValue;
     217                    }
     218
     219                    if (_currentCategory != null)
     220                    {
     221                        String localFile = Path.Combine(_currentCategory.BasePath.FullName, String.Format("{0}-{1}", aImageWPF.Id, aImageWPF.File));
     222
     223                        if (!File.Exists(localFile))
     224                        {
     225                            ThreadPool.QueueUserWorkItem((WaitCallback)delegate
     226                            {
     227                                WebRequest request = HttpWebRequest.Create(aImageWPF.UrlElement);
     228                                WebResponse response = request.GetResponse();
     229
     230                                //check the content type to assert that the file in the uri is an image
     231                                if (!response.ContentType.StartsWith("image"))
     232                                {
     233                                    throw new FileFormatException(aImageWPF.UrlElement, String.Format("Uri passed to ImageCacher does not return an image. Content is of type {0}.", response.ContentType));
     234                                }
     235
     236                                //load the image from the stream
     237                                Image image = Image.FromStream(response.GetResponseStream());
     238
     239                                //save it
     240                                image.Save(localFile);
     241
     242                            });
     243
     244                            retValue = localFile;
     245                        }
    101246                    }
    102247                    else
    103248                    {
    104                         if (!File.Exists(localFile))
    105                         {
    106                            ThreadPool.QueueUserWorkItem((WaitCallback)delegate
    107                         {
    108                             WebRequest request = HttpWebRequest.Create(aImageWPF.UrlElement);
    109                             WebResponse response = request.GetResponse();
    110 
    111                             //check the content type to assert that the file in the uri is an image
    112                            if (!response.ContentType.StartsWith("image"))
    113                            {
    114                               throw new FileFormatException(aImageWPF.UrlElement, String.Format("Uri passed to ImageCacher does not return an image. Content is of type {0}.", response.ContentType));
    115                            }
    116 
    117                            //load the image from the stream
    118                            Image image = Image.FromStream(response.GetResponseStream());
    119  
    120                            //save it
    121                            image.Save(localFile);
    122 
    123                         });
    124 
    125                         }
    126                        
    127                         _aServerDictionnary[_currentServer].Add(aImageWPF.Id,localFile);
    128                         retValue = localFile;
     249                        throw new ApplicationException("Current category not set before using image cache.");
    129250                    }
    130251                    return retValue;
    131252                }
    132         }
    133     }
    134 
     253
     254            public void Dispose()
     255            {
     256                if (_aServerDictionnary != null)
     257                {
     258                    WriteServerDictionnaryToDisk(_aServerDictionnary);
     259                }
     260            }
     261        }
     262
     263        #endregion singleton base class
    135264}
     265
     266}
Note: See TracChangeset for help on using the changeset viewer.