Exemple #1
0
        private static IList <TSource> Sort <TSource, TKey>(IList <TSource> items, IList <TKey> keys,
                                                            int begin, int end, int insertionSortCount, IComparer <TKey> comparer)
        {
            for (int i = begin; i < end; i += insertionSortCount)
            {
                InsertionSort.Sort(items, keys, i, Math.Min(i + insertionSortCount, end), comparer);
            }

            IList <TSource> srcValues  = items;
            IList <TKey>    srcKeys    = keys;
            IList <TSource> destValues = new TSource[items.Count];
            IList <TKey>    destKeys   = new TKey[items.Count];

            for (int i = insertionSortCount; i < end - begin; i *= 2)
            {
                for (int j = begin; j < end; j += i * 2)
                {
                    Merge(srcValues, srcKeys, destValues, destKeys, j, i, end, comparer);
                }

                StdUtils.Swap(ref srcValues, ref destValues);
                StdUtils.Swap(ref srcKeys, ref destKeys);
            }

            return(srcValues);
        }
        private async void TblErrorFiles_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            IList <ErrorFilePair> pairs = (IList <ErrorFilePair>)((FrameworkElement)sender).DataContext;

            if (pairs.Count == 0)
            {
                await DialogUtils.ShowSafeAsync("<none>", "Errors");

                return;
            }

            int i = 0;

            while (true)
            {
                ErrorFilePair errorPair = pairs[i];
                string        title     = $"Error: {i + 1} / {pairs.Count}";
                string        message   = $"{errorPair.Pair.RelativePath}\r\n{errorPair.Exception}";

                ContentDialogResult result = await DialogUtils.ShowContentAsync(message, title, "Cancel", "Previous", "Next", ContentDialogButton.Secondary);

                if (result == ContentDialogResult.Primary)
                {
                    i = StdUtils.OffsetIndex(i, pairs.Count, -1).index;
                }
                else if (result == ContentDialogResult.Secondary)
                {
                    i = StdUtils.OffsetIndex(i, pairs.Count, 1).index;
                }
                else
                {
                    break;
                }
            }
        }
 protected virtual bool SerialzeObject(string propertyName, object value)
 {
     try
     {
         return(SetValue(propertyName, value != null ? StdUtils.XmlSerialize(value) : string.Empty));
     }
     catch
     {
         return(false);
     }
 }
        public static T ElementAtCycle <T>(this IEnumerable <T> source, int index, int count)
        {
            index = StdUtils.CycleIndex(index, count);

            if (source is IList <T> list)
            {
                return(list[index]);
            }

            return(source.ElementAt(index));
        }
        public static T ElementAtCycle <T>(this ICollection <T> source, int index)
        {
            index = StdUtils.CycleIndex(index, source.Count);

            if (source is IList <T> list)
            {
                return(list[index]);
            }

            return(source.ElementAt(index));
        }
 private void SaveSyncs()
 {
     try
     {
         StdUtils.XmlSerialize(syncsFilePath, viewModel.Syncs);
     }
     catch (Exception e)
     {
         Settings.Current.OnSyncException(new Exception("Save syncs error", e));
     }
 }
        private void BtnInfo_Click(object sender, RoutedEventArgs e)
        {
            FileSystemImage img  = viewModel.CurrentImage;
            BitmapSource    crop = (BitmapSource)img?.CroppedImage;
            string          text =
                $"Image: {img?.Image?.PixelWidth} x {img?.Image?.PixelHeight}\r\n" +
                $"Crop: {crop?.PixelWidth ?? -1} x {crop?.PixelHeight ?? -1}\r\n" +
                $"Size: {StdUtils.GetFormattedFileSize(img?.File.Length ?? -1)}";

            MessageBox.Show(text);
        }
        private static bool AskCopy(FileInfo srcFile, string destFileName)
        {
            FileInfo destFile = new FileInfo(destFileName);
            string   message  =
                $"Replace file?\r\nSource: {srcFile.FullName}\r\n" +
                $"Size {StdUtils.GetFormattedFileSize(srcFile.Length)}\r\n" +
                $"Destination: {destFileName}\r\n" +
                $"Size {StdUtils.GetFormattedFileSize(destFile.Length)}";

            return(MessageBox.Show(message, "Replace?", MessageBoxButton.YesNo,
                                   MessageBoxImage.Warning, MessageBoxResult.No) == MessageBoxResult.Yes);
        }
Exemple #9
0
        private void TbxSearch_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            IAudioService service = viewModel.AudioServiceUI;

            if (service == null)
            {
                return;
            }

            e.Handled = true;

            switch (e.Key)
            {
            case Key.Enter:
                if (lbxSongs.SelectedItem is Song)
                {
                    bool prepend = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
                    Song addSong = (Song)lbxSongs.SelectedItem;
                    viewModel.AudioServiceUI.AddSongsToFirstPlaylist(new Song[] { addSong }, prepend);
                    service.PlayState = PlaybackState.Playing;
                }
                break;

            case Key.Escape:
                service.SearchKey = string.Empty;
                break;

            case Key.Up:
                if (lbxSongs.Items.Count > 0 && viewModel.AudioServiceUI?.IsSearching == true)
                {
                    isChangingSelectedSongIndex = true;
                    lbxSongs.SelectedIndex      =
                        StdUtils.OffsetIndex(lbxSongs.SelectedIndex, lbxSongs.Items.Count, -1).index;
                    isChangingSelectedSongIndex = false;
                }
                break;

            case Key.Down:
                if (lbxSongs.Items.Count > 0 && viewModel.AudioServiceUI?.IsSearching == true)
                {
                    isChangingSelectedSongIndex = true;
                    lbxSongs.SelectedIndex      =
                        StdUtils.OffsetIndex(lbxSongs.SelectedIndex, lbxSongs.Items.Count, 1).index;
                    isChangingSelectedSongIndex = false;
                }
                break;

            default:
                e.Handled = false;
                break;
            }
        }
        private static bool AskDelete(FileInfo file)
        {
            if (file == null)
            {
                return(false);
            }

            string size    = StdUtils.GetFormattedFileSize(file.Length);
            string message = $"Delete?\r\n{file.FullName}\r\nSize: {size}";

            return(MessageBox.Show(message, "Delete?", MessageBoxButton.YesNo,
                                   MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes);
        }
        private static void Move <T>(ObservableCollection <T> collection, T item, int offset)
        {
            int oldIndex = collection.IndexOf(item);

            if (oldIndex == -1)
            {
                return;
            }

            int newIndex = StdUtils.OffsetIndex(oldIndex, collection.Count, offset).index;

            collection.Move(oldIndex, newIndex);
        }
Exemple #12
0
        public void Restore()
        {
            string            filePath = GetFilePath(settings, window);
            RestoreWindowData data     = StdUtils.XmlDeserializeFile <RestoreWindowData>(filePath);

            FitRestoreData(data);

            WindowState restoreState = settings.OverrideMinimized.HasValue && data.WindowState == WindowState.Minimized ?
                                       settings.OverrideMinimized.Value : data.WindowState;

            if (settings.RestoreWindowState && !window.IsLoaded)
            {
                window.WindowState = restoreState == WindowState.Normal ? WindowState.Normal : WindowState.Minimized;
            }

            if (settings.RestoreLeft)
            {
                window.Left = data.Left;
            }
            if (settings.RestoreTop)
            {
                window.Top = data.Top;
            }
            if (settings.RestoreWidth)
            {
                window.Width = data.Width;
            }
            if (settings.RestoreHeight)
            {
                window.Height = data.Height;
            }

            if (settings.RestoreWindowState)
            {
                if (!window.IsLoaded)
                {
                    window.Loaded += Window_Loaded;
                }
                else
                {
                    window.WindowState = restoreState;
                }
            }

            void Window_Loaded(object sender, RoutedEventArgs e)
            {
                window.Loaded -= Window_Loaded;

                window.WindowState = restoreState;
            }
        }
Exemple #13
0
        public void Store()
        {
            string            filePath = GetFilePath(settings, window);
            RestoreWindowData data     = new RestoreWindowData()
            {
                Left        = window.RestoreBounds.Left,
                Top         = window.RestoreBounds.Top,
                Width       = window.RestoreBounds.Width,
                Height      = window.RestoreBounds.Height,
                WindowState = window.WindowState,
            };

            StdUtils.XmlSerialize(filePath, data);
        }
        protected virtual bool TryDeserialzeObject <T>(string propertyName, out T value)
        {
            try
            {
                string xml;
                if (TryGetValue(propertyName, out xml))
                {
                    value = xml.Length > 0 ? StdUtils.XmlDeserializeText <T>(xml) : default(T);
                    return(true);
                }

                value = default(T);
                return(false);
            }
            catch
            {
                value = default(T);
                return(false);
            }
        }
Exemple #15
0
        public static IList <TSource> Sort <TSource>(IList <TSource> items, int begin, int end, int insertionSortCount, IComparer <TSource> comparer)
        {
            for (int i = begin; i < end; i += insertionSortCount)
            {
                InsertionSort.Sort(items, i, Math.Min(i + insertionSortCount, end), comparer);
            }

            IList <TSource> src  = items;
            IList <TSource> dest = new TSource[items.Count];

            for (int i = insertionSortCount; i < end - begin; i *= 2)
            {
                for (int j = begin; j < end; j += i * 2)
                {
                    Merge(src, dest, j, i, end, comparer);
                }

                StdUtils.Swap(ref src, ref dest);
            }

            return(src);
        }
        private void Save()
        {
            AudioServiceData data = new AudioServiceData(Service);

            StdUtils.XmlSerialize(path, data);
        }
        private ViewModel CreateViewModel()
        {
            SyncPair[] syncs = StdUtils.XmlDeserializeFileOrDefault <SyncPair[]>(syncsFilePath) ?? new SyncPair[0];

            return(new ViewModel(syncs));
        }
 public object Convert(object value, Type targetType, object parameter, string language)
 {
     return(StdUtils.ToString(TimeSpan.FromSeconds((double)value)));
 }
        public static async Task <IList <TSource> > Sort <TSource>(IList <TSource> items,
                                                                   int begin, int end, int insertionSortCount, int threadCount, IComparer <TSource> comparer)
        {
            int            insertionSortActionsCount = (int)Math.Ceiling((end - begin) / (double)insertionSortCount);
            int            depth      = (int)Math.Ceiling(Math.Log(insertionSortActionsCount, 2));
            int            donesCount = 0;
            IList <bool>   dones      = new bool[2 * insertionSortActionsCount + depth];
            Queue <Action> queue      = new Queue <Action>();

            for (int i = begin; i < end; i += insertionSortCount)
            {
                int index          = donesCount++;
                int insertionBegin = i;
                int insertionEnd   = Math.Min(i + insertionSortCount, end);

                queue.Enqueue(() => InsertionSort(index, insertionBegin, insertionEnd));
            }

            IList <TSource> items1 = items;
            IList <TSource> items2 = new TSource[items.Count];

            for (int range = insertionSortCount; range < end - begin; range *= 2)
            {
                int             lastActionsCount = (int)Math.Ceiling((end - begin) / (double)range);
                int             lastActionsBegin = queue.Count - lastActionsCount;
                int             currentRange = range;
                IList <TSource> src = items1, dest = items2;

                for (int j = 0, rangeBegin = begin; rangeBegin < end; j++, rangeBegin += range * 2)
                {
                    int leftNodeIndex  = lastActionsBegin + j * 2;
                    int rightNodeIndex = lastActionsBegin + Math.Min(j * 2 + 1, lastActionsCount);
                    int index          = donesCount++;
                    int currentBegin   = rangeBegin;

                    queue.Enqueue(() => Merge(leftNodeIndex, rightNodeIndex, index, src, dest, currentBegin, currentRange));
                }

                StdUtils.Swap(ref items1, ref items2);
            }

            await AsyncUtils.ParallelForEach(queue, threadCount);

            return(items1);

            void InsertionSort(int thisIndex, int insertionSortBegin, int insertionSortEnd)
            {
                Linq.Sort.InsertionSort.Sort(items, insertionSortBegin, insertionSortEnd, comparer);

                lock (dones)
                {
                    dones[thisIndex] = true;
                    Monitor.PulseAll(dones);
                }
            }

            void Merge(int leftIndex, int rightIndex, int thisIndex,
                       IList <TSource> mergeSrc, IList <TSource> mergeDest, int mergeBegin, int size)
            {
                lock (dones)
                {
                    while (!dones[leftIndex])
                    {
                        Monitor.Wait(dones);
                    }
                    while (!dones[rightIndex])
                    {
                        Monitor.Wait(dones);
                    }
                }

                MergeSort.Merge(mergeSrc, mergeDest, mergeBegin, size, end, comparer);
                lock (dones)
                {
                    dones[thisIndex] = true;
                    Monitor.PulseAll(dones);
                }
            }
        }
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     return(StdUtils.ToString((TimeSpan)value));
 }
        private void Load()
        {
            AudioServiceData data;

            if (!File.Exists(path))
            {
                return;
            }

            try
            {
                data = StdUtils.XmlDeserializeFile <AudioServiceData>(path);
            }
            catch
            {
                return;
            }

            Service.Volume = data.Volume;

            IDictionary <string, Song> allSongs = new Dictionary <string, Song>();

            Service.SourcePlaylists = data.SourcePlaylists.Select(playlistData =>
            {
                Guid id = Guid.Parse(playlistData.ID);
                ISourcePlaylistBase playlist = Service.SourcePlaylists
                                               .FirstOrDefault(s => s.ID == id) ?? Service.CreateSourcePlaylist(id);

                MergePlaylist(playlist, playlistData);

                foreach (Song song in playlist.Songs)
                {
                    allSongs[song.FullPath] = song;
                }

                return(playlist);
            }).ToArray();

            Service.Playlists = data.Playlists.Select(playlistData =>
            {
                Guid id = Guid.Parse(playlistData.ID);
                IPlaylistBase playlist = Service.Playlists
                                         .FirstOrDefault(s => s.ID == id) ?? Service.CreatePlaylist(id);

                playlistData.Songs = playlistData.Songs.Where(s => allSongs.ContainsKey(s.FullPath)).ToArray();

                MergePlaylist(playlist, playlistData);

                return(playlist);
            }).ToArray();

            if (string.IsNullOrWhiteSpace(data.CurrentPlaylistID))
            {
                Service.CurrentPlaylist = null;
            }
            else
            {
                Guid currentPlaylistID = Guid.Parse(data.CurrentPlaylistID);
                Service.CurrentPlaylist = Service.GetAllPlaylists().FirstOrDefault(p => p.ID == currentPlaylistID);
            }
        }