Exemplo n.º 1
0
        private void populateListViewFromDirectory(MsDataFileUri directory)
        {
            _abortPopulateList = false;
            listView.Cursor    = Cursors.Default;
            _waitingForData    = false;
            listView.Items.Clear();

            var listSourceInfo = new List <SourceInfo>();

            if (null == directory || directory is MsDataFilePath && string.IsNullOrEmpty(((MsDataFilePath)directory).FilePath))
            {
                foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
                {
                    string     label      = string.Empty;
                    string     sublabel   = driveInfo.Name;
                    ImageIndex imageIndex = ImageIndex.Folder;
                    _driveReadiness[sublabel] = false;
                    try
                    {
                        switch (driveInfo.DriveType)
                        {
                        case DriveType.Fixed:
                            imageIndex = ImageIndex.LocalDrive;
                            label      = Resources.OpenDataSourceDialog_populateListViewFromDirectory_Local_Drive;
                            if (driveInfo.VolumeLabel.Length > 0)
                            {
                                label = driveInfo.VolumeLabel;
                            }
                            break;

                        case DriveType.CDRom:
                            imageIndex = ImageIndex.OpticalDrive;
                            label      = Resources.OpenDataSourceDialog_populateListViewFromDirectory_Optical_Drive;
                            if (driveInfo.IsReady && driveInfo.VolumeLabel.Length > 0)
                            {
                                label = driveInfo.VolumeLabel;
                            }
                            break;

                        case DriveType.Removable:
                            imageIndex = ImageIndex.OpticalDrive;
                            label      = Resources.OpenDataSourceDialog_populateListViewFromDirectory_Removable_Drive;
                            if (driveInfo.IsReady && driveInfo.VolumeLabel.Length > 0)
                            {
                                label = driveInfo.VolumeLabel;
                            }
                            break;

                        case DriveType.Network:
                            label = Resources.OpenDataSourceDialog_populateListViewFromDirectory_Network_Share;
                            break;
                        }
                        _driveReadiness[sublabel] = IsDriveReady(driveInfo);
                    }
                    catch (Exception)
                    {
                        label += string.Format(@" ({0})", Resources.OpenDataSourceDialog_populateListViewFromDirectory_access_failure);
                    }

                    string name = driveInfo.Name;
                    if (label != string.Empty)
                    {
                        name = string.Format(@"{0} ({1})", label, name);
                    }

                    listSourceInfo.Add(new SourceInfo(new MsDataFilePath(driveInfo.RootDirectory.FullName))
                    {
                        type         = DataSourceUtil.FOLDER_TYPE,
                        imageIndex   = imageIndex,
                        name         = name,
                        dateModified = GetDriveModifiedTime(driveInfo)
                    });
                }
            }
            else if (directory is RemoteUrl)
            {
                RemoteUrl remoteUrl = directory as RemoteUrl;
                if (string.IsNullOrEmpty(remoteUrl.ServerUrl))
                {
                    foreach (var remoteAccount in _remoteAccounts)
                    {
                        listSourceInfo.Add(new SourceInfo(remoteAccount.GetRootUrl())
                        {
                            name       = remoteAccount.GetKey(),
                            type       = DataSourceUtil.FOLDER_TYPE,
                            imageIndex = ImageIndex.MyNetworkPlaces,
                        });
                    }
                }
                else
                {
                    RemoteAccount remoteAccount = GetRemoteAccount(remoteUrl);
                    if (RemoteSession == null || !Equals(remoteAccount, RemoteSession.Account))
                    {
                        RemoteSession = RemoteSession.CreateSession(remoteAccount);
                    }
                    RemoteServerException exception;
                    bool isComplete = _remoteSession.AsyncFetchContents(remoteUrl, out exception);
                    foreach (var item in _remoteSession.ListContents(remoteUrl))
                    {
                        var imageIndex = DataSourceUtil.IsFolderType(item.Type)
                            ? ImageIndex.Folder
                            : ImageIndex.MassSpecFile;
                        listSourceInfo.Add(new SourceInfo(item.MsDataFileUri)
                        {
                            name         = item.Label,
                            type         = item.Type,
                            imageIndex   = imageIndex,
                            dateModified = item.LastModified,
                            size         = item.FileSize
                        });
                    }
                    if (null != exception)
                    {
                        if (MultiButtonMsgDlg.Show(this, exception.Message, Resources.OpenDataSourceDialog_populateListViewFromDirectory_Retry) != DialogResult.Cancel)
                        {
                            RemoteSession.RetryFetchContents(remoteUrl);
                            isComplete = false;
                        }
                    }
                    if (!isComplete)
                    {
                        listView.Cursor = Cursors.WaitCursor;
                        _waitingForData = true;
                    }
                }
            }
            else if (directory is MsDataFilePath)
            {
                MsDataFilePath msDataFilePath = (MsDataFilePath)directory;
                DirectoryInfo  dirInfo        = new DirectoryInfo(msDataFilePath.FilePath);

                try
                {
                    // subitems: Name, Type, Spectra, Size, Date Modified
                    var arraySubDirInfo = dirInfo.GetDirectories();
                    Array.Sort(arraySubDirInfo, (d1, d2) => string.Compare(d1.Name, d2.Name, StringComparison.CurrentCultureIgnoreCase));
                    var arrayFileInfo = dirInfo.GetFiles();
                    Array.Sort(arrayFileInfo, (f1, f2) => string.Compare(f1.Name, f2.Name, StringComparison.CurrentCultureIgnoreCase));

                    // Calculate information about the files, allowing the user to cancel
                    foreach (var info in arraySubDirInfo)
                    {
                        listSourceInfo.Add(getSourceInfo(info));
                        Application.DoEvents();
                        if (_abortPopulateList)
                        {
                            //MessageBox.Show( "abort" );
                            break;
                        }
                    }

                    if (!_abortPopulateList)
                    {
                        foreach (var info in arrayFileInfo)
                        {
                            listSourceInfo.Add(getSourceInfo(info));
                            Application.DoEvents();
                            if (_abortPopulateList)
                            {
                                //MessageBox.Show( "abort" );
                                break;
                            }
                        }
                    }
                }
                catch (Exception x)
                {
                    var message = TextUtil.LineSeparate(
                        Resources.OpenDataSourceDialog_populateListViewFromDirectory_An_error_occurred_attempting_to_retrieve_the_contents_of_this_directory,
                        x.Message);
                    // Might throw access violation.
                    MessageDlg.ShowWithException(this, message, x);
                    return;
                }
            }

            // Populate the list
            var items = new List <ListViewItem>();

            foreach (var sourceInfo in listSourceInfo)
            {
                if (sourceInfo != null &&
                    (sourceTypeComboBox.SelectedIndex == 0 ||
                     sourceTypeComboBox.SelectedItem.ToString() == sourceInfo.type ||
                     // Always show folders
                     sourceInfo.isFolder))
                {
                    ListViewItem item = new ListViewItem(sourceInfo.ToArray(), (int)sourceInfo.imageIndex)
                    {
                        Tag = sourceInfo,
                    };
                    item.SubItems[2].Tag = sourceInfo.size;
                    item.SubItems[3].Tag = sourceInfo.dateModified;

                    items.Add(item);
                }
            }
            listView.Items.AddRange(items.ToArray());
        }