Пример #1
0
        private void AddNew()
        {
            var dlg = new OpenFileDialog();

            dlg.Filter      = "All files (*.*)|*.*";
            dlg.Multiselect = true;
            if (dlg.ShowDialog().ValueOrFalse())
            {
                foreach (string filename in dlg.FileNames)
                {
                    FileInfo finfo = new FileInfo(filename);
                    if (finfo.Exists)
                    {
                        MultimediaLink          model     = null;
                        MultimediaLinkViewModel viewModel = null;
                        Multimedia duplicate = null;
                        var        action    = CheckDuplicate(finfo, out duplicate);
                        switch (action)
                        {
                        case MultimediaDuplicateAction.Cancel:
                            // Do nothing
                            break;

                        case MultimediaDuplicateAction.NoDuplicate:
                        case MultimediaDuplicateAction.InsertDuplicate:
                            // Insert new multimedia and new link
                            model = new MultimediaLink();
                            model.MultimediaID     = NextNewId();
                            model.MultimediaLinkID = model.MultimediaID;
                            if (finfo.Name.Contains("."))
                            {
                                model.Name      = finfo.Name.Substring(0, finfo.Name.LastIndexOf("."));
                                model.Extension = finfo.Extension.Substring(1);
                            }
                            else
                            {
                                model.Name = finfo.Name;
                            }
                            viewModel = new MultimediaLinkViewModel(model);
                            _tempFileManager.CopyToTempFile(viewModel.MultimediaID, filename);
                            _model.Add(viewModel);
                            RegisterPendingChange(new InsertMultimediaCommand(model, _tempFileManager.GetContentFileName(viewModel.MultimediaID, finfo.Extension.Substring(1))));
                            break;

                        case MultimediaDuplicateAction.UseExisting:
                            // Should never get here!
                            break;

                        case MultimediaDuplicateAction.ReplaceExisting:
                            // register an update for the multimedia,
                            // and insert a new link
                            // Link to existing multimedia
                            model = new MultimediaLink();
                            model.MultimediaID     = duplicate.MultimediaID;
                            model.MultimediaLinkID = -1;
                            model.Name             = duplicate.Name;
                            model.Extension        = duplicate.FileExtension;
                            viewModel = new MultimediaLinkViewModel(model);
                            _model.Add(viewModel);
                            _tempFileManager.CopyToTempFile(viewModel.MultimediaID, filename);
                            RegisterPendingChange(new UpdateMultimediaBytesCommand(model, filename));
                            break;
                        }

                        if (viewModel != null)
                        {
                            viewModel.IsSelected = true;
                        }
                    }
                }
            }
        }
Пример #2
0
        private void AddMultimediaFromFile(string filename)
        {
            if (IsReadOnly)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(filename))
            {
                return;
            }

            FileInfo finfo = new FileInfo(filename);

            if (finfo.Exists)
            {
                MultimediaLink          model     = null;
                MultimediaLinkViewModel viewModel = null;
                Multimedia duplicate = null;
                var        action    = CheckDuplicate(finfo, out duplicate);
                switch (action)
                {
                case MultimediaDuplicateAction.Cancel:
                    // Do nothing
                    break;

                case MultimediaDuplicateAction.NoDuplicate:
                case MultimediaDuplicateAction.InsertDuplicate:
                    // Insert new multimedia and new link
                    model = new MultimediaLink();
                    model.MultimediaID     = NextNewId();
                    model.MultimediaLinkID = model.MultimediaID;
                    if (finfo.Name.Contains("."))
                    {
                        model.Name      = finfo.Name.Substring(0, finfo.Name.LastIndexOf("."));
                        model.Extension = finfo.Extension.Substring(1);
                    }
                    else
                    {
                        model.Name = finfo.Name;
                    }
                    model.SizeInBytes = (int)finfo.Length;

                    viewModel           = new MultimediaLinkViewModel(model);
                    viewModel.Thumbnail = GraphicsUtils.GenerateThumbnail(filename, THUMB_SIZE);
                    _tempFileManager.CopyToTempFile(viewModel.MultimediaID, filename);
                    _model.Add(viewModel);
                    RegisterPendingChange(new InsertMultimediaCommand(model, _tempFileManager.GetContentFileName(viewModel.MultimediaID, finfo.Extension.Substring(1))));
                    RegisterPendingChange(new InsertMultimediaLinkCommand(model, CategoryType, Owner));
                    break;

                case MultimediaDuplicateAction.UseExisting:
                    // Link to existing multimedia
                    model = new MultimediaLink();
                    model.MultimediaID     = duplicate.MultimediaID;
                    model.MultimediaLinkID = NewLinkID();
                    model.Name             = duplicate.Name;
                    model.Extension        = duplicate.FileExtension;
                    viewModel = new MultimediaLinkViewModel(model);
                    GenerateThumbnail(viewModel, THUMB_SIZE);
                    _model.Add(viewModel);
                    RegisterPendingChange(new InsertMultimediaLinkCommand(model, CategoryType, Owner));
                    break;

                case MultimediaDuplicateAction.ReplaceExisting:
                    // register an update for the multimedia,
                    // and insert a new link
                    // Link to existing multimedia
                    model = new MultimediaLink();
                    model.MultimediaID     = duplicate.MultimediaID;
                    model.MultimediaLinkID = NewLinkID();
                    model.Name             = duplicate.Name;
                    model.Extension        = duplicate.FileExtension;
                    viewModel = new MultimediaLinkViewModel(model);
                    GenerateThumbnail(viewModel, THUMB_SIZE);
                    _model.Add(viewModel);
                    _tempFileManager.CopyToTempFile(viewModel.MultimediaID, filename);
                    RegisterPendingChange(new UpdateMultimediaBytesCommand(model, filename));
                    RegisterPendingChange(new InsertMultimediaLinkCommand(model, CategoryType, Owner));
                    break;
                }

                if (viewModel != null)
                {
                    viewModel.IsSelected   = true;
                    thumbList.SelectedItem = viewModel;
                }
            }
        }