public void LogUserLogin( User user )
 {
     Statistics.Add( AuthenticationStatisticItem.CreateUserLoggedEvent( user ) );
 }
 public void LogAppExit( User user )
 {
     Statistics.Add( AuthenticationStatisticItem.CreateAppExitEvent( user ) );
 }
 public void LogUserDelete( User user )
 {
     Statistics.Add( AuthenticationStatisticItem.CreateUserDeleteEvent( user ) );
 }
        private User CreateUser( string username, string password )
        {
            User newUser = new User( username, password );
            UserDatabase.Users.Add( newUser );

            using( IsolatedStorageFile appDirectory = IsolatedStorageFile.GetUserStoreForApplication() )
            {
                appDirectory.CreateDirectory( newUser.LocalId.ToString() );
                appDirectory.CreateDirectory( "shared/transfers/" + newUser.LocalId.ToString() );
            }

            return newUser;
        }
        public IEnumerable<StatisticItem> GetStatisticsForSending( User user, Guid updateId )
        {
            List<StatisticItem> statsToSend = new List<StatisticItem>();
            if( user == null )
                return statsToSend;

            foreach( StatisticItem stat in Statistics.Where( stat => stat.Username == user.Username ).Where( stat => stat.UpdateId == Guid.Empty ) )
            {
                stat.UpdateId = updateId;
                statsToSend.Add( stat );

            }
            return statsToSend;
        }
        private static void updateXml(XDocument newXml, Library library, User owner)
        {
            //step 2 cleanup empty categories
            var emptyCategories =
                from newElement in newXml.Descendants(LibraryModel.NedNodeTag)
                where newElement.Attribute(LibraryModel.NedNodeTypeAttribute).Value == LibraryModel.CategoryTagType
                    && newElement.Descendants(LibraryModel.NedNodeTag).Count() == 0
                select newElement;
            for ( int i = emptyCategories.Count() - 1; i >= 0; i--)
            {
                XElement category = emptyCategories.ElementAt(i);
                string categoryToDeleteId = category.Attribute(LibraryModel.NedNodeIdAttribute).Value;
                foreach (CategoryModelItem categoryModelItem in App.Engine.LibraryModel.CategoryItems)
                {
                    if (categoryModelItem.Id == categoryToDeleteId)
                    {
                        categoryModelItem.IsChanged = false;
                        break;
                    }
                }
                category.Remove();
            }

            //step 3 cleanup empty catalogues
            var emptyCatalogs =
                from newElement in newXml.Descendants(LibraryModel.NedNodeTag)
                where newElement.Attribute(LibraryModel.NedNodeTypeAttribute).Value == LibraryModel.CatalogueTagType
                    && newElement.Descendants(LibraryModel.NedNodeTag).Count() == 0
                select newElement;

            for (int i = emptyCatalogs.Count() - 1; i >= 0; i--)
            {
                XElement catalogue = emptyCatalogs.ElementAt(i);
                string catalogueToDeleteId = catalogue.Attribute(LibraryModel.NedNodeIdAttribute).Value;
                foreach (CatalogueModelItem catalogueModelItem in App.Engine.LibraryModel.CatalogueItems)
                {
                    if (catalogueModelItem.Id == catalogueToDeleteId)
                    {
                        catalogueModelItem.IsChanged = false;
                        break;
                    }
                }
                catalogue.Remove();
            }

            //step 4 save diff xml and remove previous library xml
            using (IsolatedStorageFile appDirectory = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (newXml.Root != null)
                {
                    using (var stream = appDirectory.OpenFile(Utils.LibraryXmlDiffPath(owner, library), FileMode.Create))
                    {
                        newXml.Save(stream);
                    }
                }
                else
                {
                    if (appDirectory.FileExists(Utils.LibraryXmlDiffPath(owner, library)))
                    {
                        appDirectory.DeleteFile(Utils.LibraryXmlDiffPath(owner, library));
                    }
                }
                if (appDirectory.FileExists(Utils.LibraryXmlPreviousPath(owner, library)))
                {
                    appDirectory.DeleteFile(Utils.LibraryXmlPreviousPath(owner, library));
                }
            }
        }
        public static void SaveLibraryContents(string updatedContents, Library libToUpdate, User owner)
        {
            using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                string libraryDirectoryPath = Utils.LibraryDirPath(owner, libToUpdate);
                if (!isolatedStorage.DirectoryExists(libraryDirectoryPath))
                {
                    isolatedStorage.CreateDirectory(libraryDirectoryPath);
                    isolatedStorage.CreateDirectory("shared/transfers/" + libraryDirectoryPath);
                }

                using (StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(Utils.LibraryXmlPath(owner, libToUpdate), FileMode.Create, isolatedStorage)))
                {
                    writer.Write(updatedContents);
                }
            }
        }
        public static void PrepareDiffXml(Library library, User owner)
        {
            XDocument oldLib;
            XDocument newLib;
            XDocument oldDiff = null;
            using (IsolatedStorageFile appDirectory = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if(!appDirectory.FileExists(Utils.LibraryXmlPreviousPath(owner, library)))
                {
                    return;
                }
                using (var stream = appDirectory.OpenFile(Utils.LibraryXmlPreviousPath(owner, library), FileMode.Open, FileAccess.Read))
                {
                    oldLib = XDocument.Load(stream);
                }
                using (var stream = appDirectory.OpenFile(Utils.LibraryXmlPath(owner, library), FileMode.Open, FileAccess.Read))
                {
                    newLib = XDocument.Load(stream);
                }
                if(appDirectory.FileExists(Utils.LibraryXmlDiffPath(owner, library)))
                {
                    using (var stream = appDirectory.OpenFile(Utils.LibraryXmlDiffPath(owner, library), FileMode.Open, FileAccess.Read))
                    {
                        oldDiff = XDocument.Load(stream);
                    }
                }
            }

            XElement changesRoot = new XElement("changes");
            XDocument changes = new XDocument(changesRoot);

            List<XElement> newNodes = new List<XElement>(newLib.Descendants(LibraryModel.NedNodeTag));

            //step 1 - leave in newLib xml only changed elements
            for( int i = newNodes.Count - 1; i>= 0; i --)
            {
                XElement newNode = newNodes[i];
                var oldNodesSearch =
                    from oldElements in oldLib.Descendants(LibraryModel.NedNodeTag)
                    where (string)oldElements.Attribute(LibraryModel.NedNodeIdAttribute) == (string)newNode.Attribute(LibraryModel.NedNodeIdAttribute)
                    select oldElements;

                if (oldNodesSearch.Count() > 0)
                {
                    if (areItemsEqual(newNode, oldNodesSearch.First()) &&  newNode.Descendants(LibraryModel.NedNodeTag).Count() == 0)
                    {
                        if(oldDiff == null || (from oldDiffNode in oldDiff.Root.Descendants(LibraryModel.NedNodeTag)
                                                   where oldDiffNode.Attribute(LibraryModel.NedNodeIdAttribute).Value == newNode.Attribute(LibraryModel.NedNodeIdAttribute).Value
                                                   select oldDiff).Count() <= 0)
                        newNode.Remove();
                    }
                }
            }

            updateXml(newLib, library, owner);
        }
 public static XDocument GetLibraryContents(Library libToLoad, User owner)
 {
     XDocument result = null;
     using (IsolatedStorageFile appDirectory = IsolatedStorageFile.GetUserStoreForApplication())
     using (var stream = appDirectory.OpenFile(Utils.LibraryXmlPath(owner, libToLoad), FileMode.Open, FileAccess.Read))
     {
         result = XDocument.Load(stream);
     }
     return result;
 }
 public static XDocument GetChangedContent(Library library, User user)
 {
     using (IsolatedStorageFile appDirectory = IsolatedStorageFile.GetUserStoreForApplication())
     {
         if (appDirectory.FileExists(Utils.LibraryXmlDiffPath(user, library)))
         {
             using (var stream = appDirectory.OpenFile(Utils.LibraryXmlDiffPath(user, library), FileMode.Open, FileAccess.Read))
             {
                 return XDocument.Load(stream);
             }
         }
         return null;
     }
 }