private async Task DownloadFile(SDCardListItem item)
        {
            var indicator = SystemTray.GetProgressIndicator(this);

            indicator.IsIndeterminate = true;
            indicator.Text            = String.Format(AppResources.DownloadingProgressText, item.Name);
            try
            {
                Stream s = await item.ThisFile.OpenForReadAsync();

                if (s != null)
                {
                    item.Stream = s;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format(AppResources.DownloadErrorText, item.Name, ex.Message), AppResources.ErrorCaption, MessageBoxButton.OK);
            }

#if GBC
            indicator.Text = AppResources.ApplicationTitle2;
#else
            indicator.Text = AppResources.ApplicationTitle;
#endif
            indicator.IsIndeterminate = false;

            item.Downloading = false;
        }
        private List <SDCardListItem> GetFilesInArchive(SDCardListItem item)
        {
            List <SDCardListItem> listItems = new List <SDCardListItem>();


            if (item.Stream != null)
            {
                //fix SD card stream bug
                Stream s = new MemoryStream();
                item.Stream.CopyTo(s);
                s.Position = 0;
                item.Stream.Close();// close because we copy it to s already
                item.Stream = null;

                //get list of file
                IArchive archive = null;

                if (item.Type == SkyDriveItemType.Rar)
                {
                    archive = SharpCompress.Archive.Rar.RarArchive.Open(s);
                }
                else if (item.Type == SkyDriveItemType.Zip)
                {
                    archive = SharpCompress.Archive.Zip.ZipArchive.Open(s);
                }
                else if (item.Type == SkyDriveItemType.SevenZip)
                {
                    archive = SharpCompress.Archive.SevenZip.SevenZipArchive.Open(s);
                }


                foreach (var entry in archive.Entries)
                {
                    if (!entry.IsDirectory)
                    {
                        Stream data = new MemoryStream();
                        entry.WriteTo(data);
                        data.Position = 0;
                        String name = entry.FilePath;

                        SkyDriveItemType type = SkyDriveItemType.File;
                        int dotIndex          = -1;
                        if ((dotIndex = name.LastIndexOf('.')) != -1)
                        {
                            String substrName = name.Substring(dotIndex).ToLower();
                            if (substrName.Equals(".gb") || substrName.Equals(".gbc") || substrName.Equals(".gba"))
                            {
                                type = SkyDriveItemType.ROM;
                            }
                            else if (substrName.Equals(".sgm"))
                            {
                                type = SkyDriveItemType.Savestate;
                            }
                            else if (substrName.Equals(".sav"))
                            {
                                type = SkyDriveItemType.SRAM;
                            }
                        }

                        if (type == SkyDriveItemType.File)
                        {
                            data.Close();
                            continue;
                        }

                        SDCardListItem listItem = new SDCardListItem()
                        {
                            Name       = name,
                            Type       = type,
                            isFolder   = false,
                            ParentPath = item.ThisFile.Path,
                            Stream     = data
                        };

                        listItems.Add(listItem);
                    }
                }

                //close the zip stream since we have the stream of each item inside it already
                s.Close();
            }

            return(listItems);
        }
        async void skydriveList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SDCardListItem item = this.skydriveList.SelectedItem as SDCardListItem;

            if (item == null)
            {
                return;
            }

            ROMDatabase db = ROMDatabase.Current;

            if (item.isFolder)
            {
                this.skydriveList.ItemsSource = null;
                this.currentFolderBox.Text    = item.Name;
                this.OpenFolder(item.ThisFolder);
            }

            else  //file
            {
                if (item.Type == SkyDriveItemType.Zip || item.Type == SkyDriveItemType.Rar || item.Type == SkyDriveItemType.SevenZip)
                {
                    this.skydriveList.ItemsSource = null;
                    this.currentFolderBox.Text    = item.Name;
                    this.downloadsInProgress++;

                    await this.DownloadFile(item);

                    try
                    {
                        List <SDCardListItem> listItems;

                        listItems = this.GetFilesInArchive(item);

                        this.skydriveStack.Add(listItems);
                        this.skydriveList.ItemsSource = listItems;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, AppResources.ErrorCaption, MessageBoxButton.OK);
                    }

                    this.downloadsInProgress--;
                }

                else if (item.Type == SkyDriveItemType.ROM)
                {
                    // Download
                    if (!item.Downloading)
                    {
                        this.downloadsInProgress++;
                        if (item.Stream == null)
                        {
                            await this.DownloadFile(item);
                        }


                        await SkyDriveImportPage.ImportROM(item, this);

                        this.downloadsInProgress--;
                    }
                    else
                    {
                        MessageBox.Show(AppResources.AlreadyDownloadingText, AppResources.ErrorCaption, MessageBoxButton.OK);
                    }
                }

                else if (item.Type == SkyDriveItemType.Savestate || item.Type == SkyDriveItemType.SRAM)
                {
                    //check to make sure there is a rom with matching name
                    ROMDBEntry entry = null;

                    if (item.Type == SkyDriveItemType.Savestate)  //save state
                    {
                        entry = db.GetROMFromSavestateName(item.Name);
                    }
                    else if (item.Type == SkyDriveItemType.SRAM) //sram
                    {
                        entry = db.GetROMFromSRAMName(item.Name);
                    }

                    if (entry == null) //no matching file name
                    {
                        MessageBox.Show(AppResources.NoMatchingNameText, AppResources.ErrorCaption, MessageBoxButton.OK);
                        return;
                    }

                    //check to make sure format is right
                    if (item.Type == SkyDriveItemType.Savestate)  //save state
                    {
                        string slot       = item.Name.Substring(item.Name.Length - 5, 1);
                        int    parsedSlot = 0;
                        if (!int.TryParse(slot, out parsedSlot))
                        {
                            MessageBox.Show(AppResources.ImportSavestateInvalidFormat, AppResources.ErrorCaption, MessageBoxButton.OK);
                            return;
                        }
                    }

                    // Download
                    if (!item.Downloading)
                    {
                        this.downloadsInProgress++;

                        if (item.Stream == null)
                        {
                            await this.DownloadFile(item);
                        }

                        await SkyDriveImportPage.ImportSave(item, this);

                        this.downloadsInProgress--;
                    }
                    else
                    {
                        MessageBox.Show(AppResources.AlreadyDownloadingText, AppResources.ErrorCaption, MessageBoxButton.OK);
                    }
                }
            }
        }
        async void OpenFolder(ExternalStorageFolder folderToOpen)
        {
            try
            {
                //new list
                List <SDCardListItem> listItems = new List <SDCardListItem>();


                // Get all folders on root
                List <ExternalStorageFolder> listFolders = (await folderToOpen.GetFoldersAsync()).ToList();
                foreach (ExternalStorageFolder folder in listFolders)
                {
                    SDCardListItem item = new SDCardListItem()
                    {
                        isFolder   = true,
                        Name       = folder.Name,
                        ParentPath = folderToOpen.Path,
                        ThisFolder = folder
                    };
                    listItems.Add(item);
                }

                List <ExternalStorageFile> listFiles = (await folderToOpen.GetFilesAsync()).ToList();
                foreach (ExternalStorageFile file in listFiles)
                {
                    SDCardListItem item = new SDCardListItem()
                    {
                        isFolder   = false,
                        Name       = file.Name,
                        ParentPath = folderToOpen.Path,
                        ThisFile   = file
                    };
                    if (item.ThisFile.Path.ToLower().EndsWith(".gb") || item.ThisFile.Path.ToLower().EndsWith(".gbc") || item.ThisFile.Path.ToLower().EndsWith(".gba"))
                    {
                        item.Type = SkyDriveItemType.ROM;
                    }
                    else if (item.ThisFile.Path.ToLower().EndsWith(".sav"))
                    {
                        item.Type = SkyDriveItemType.SRAM;
                    }
                    else if (item.ThisFile.Path.ToLower().EndsWith(".sgm"))
                    {
                        item.Type = SkyDriveItemType.Savestate;
                    }
                    else if (item.ThisFile.Path.ToLower().EndsWith(".zib") || item.ThisFile.Path.ToLower().EndsWith(".zip"))
                    {
                        item.Type = SkyDriveItemType.Zip;
                    }
                    else if (item.ThisFile.Path.ToLower().EndsWith(".rar"))
                    {
                        item.Type = SkyDriveItemType.Rar;
                    }
                    else if (item.ThisFile.Path.ToLower().EndsWith(".7z"))
                    {
                        item.Type = SkyDriveItemType.SevenZip;
                    }
                    else
                    {
                        item.Type = SkyDriveItemType.File;
                    }

                    listItems.Add(item);
                }

                //ordered by name
                listItems = listItems.OrderBy(x => x.Name).ToList();

                this.skydriveStack.Add(listItems);
                this.skydriveList.ItemsSource = listItems;
            }
            catch (Exception)
            {
                MessageBox.Show(AppResources.ErrorReadingSDCardText);
            }
        }