Exemplo n.º 1
0
        private bool CreateNewPlaylistFile(string strOrgPlaylistPath, string strOrgPlaylistContent, PlayListUpdatesViewModel PlaylistUpdatesEntry)
        {
            List <String> resultingFile = new List <string>();
            int           lastIndex     = 0;

            try
            {
                PlaylistChange lastPlaylistChange = null;

                foreach (PlaylistChange playlistChange in PlaylistUpdatesEntry.PlaylistChanges)
                {
                    lastPlaylistChange = playlistChange;

                    // part until changing path
                    resultingFile.Add(strOrgPlaylistContent.Substring(lastIndex, playlistChange.validPath.iStart - lastIndex));
                    lastIndex = playlistChange.validPath.iEnd + 1;

                    string orgPath = strOrgPlaylistContent.Substring(playlistChange.validPath.iStart, playlistChange.validPath.iEnd - playlistChange.validPath.iStart + 1);

                    // changing path
                    if (playlistChange.validPath.PathType == enPathType.absolute)
                    {
                        //resultingFile.Add(playlistChange.changingEntry.targetNode.otherView.GetFullPathFromNode());
                        string newPath = playlistChange.changingEntry.targetNode.GetPathAfterActionsFromNode(orgPath);

                        //if( newPath.Length == 0 )
                        //    foreach(string otherPaths in playlistChange.changingEntry.targetNode.DuplicatesSourceSourceOrTT)
                        // TBD

                        resultingFile.Add(newPath);
                    }
                    else // relative path
                    {
                        //resultingFile.Add(playlistChange.changingEntry.targetNode.otherView.GetFullPathFromNode());
                        string newPathAbsolute = playlistChange.changingEntry.targetNode.GetPathAfterActionsFromNode(orgPath);

                        //if( newPath.Length == 0 )
                        //    foreach(string otherPaths in playlistChange.changingEntry.targetNode.DuplicatesSourceSourceOrTT)
                        // TBD alternatives for delete

                        //System.IO.DirectoryInfo dirInfoPlaylistPath = new System.IO.DirectoryInfo(strOrgPlaylistPath);
                        //string strPlaylistBasePath = dirInfoPlaylistPath.FullName;

                        System.Uri uriFileAbsolute = new Uri(newPathAbsolute);
                        System.Uri uriBasePath     = new Uri(strOrgPlaylistPath);

                        System.Uri uriRelative = uriBasePath.MakeRelativeUri(uriFileAbsolute);

                        resultingFile.Add(Uri.UnescapeDataString(uriRelative.ToString()));
                    }
                }

                if (lastPlaylistChange != null)
                {
                    if (lastPlaylistChange.validPath.iEnd < strOrgPlaylistContent.Length)
                    {
                        // last part
                        resultingFile.Add(strOrgPlaylistContent.Substring(lastPlaylistChange.validPath.iEnd + 1, strOrgPlaylistContent.Length - (lastPlaylistChange.validPath.iEnd + 1)));
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Internal error: " + ex.Message);

                return(false);
            }


            try
            {
                // Write File
                if (resultingFile.Count > 0)
                {
                    System.IO.StreamWriter writer = new System.IO.StreamWriter(PlaylistUpdatesEntry.PathTemp, false, Encoding.Default);

                    foreach (string str in resultingFile)
                    {
                        writer.Write(str);
                    }

                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error trying to write " + PlaylistUpdatesEntry.PathTemp + " " + ex.Message);
            }


            return(false);
        }
Exemplo n.º 2
0
        //Own Thread
        public void ThreadSave_GeneratePlaylistUpdates() // over TaskPlannerBase
        {
            try
            {
                old_PlaylistUpdatesItems   = playlistUpdatesModel.Items;
                playlistUpdatesModel.Items = new ObservableCollection <PlayListUpdatesViewModel>(); // Reset


                // Delete old Plalist Updates
                String TempPlaylistPath = TempPlaylistPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

                if (System.IO.Directory.Exists(TempPlaylistPath + "\\RoMoRDuP\\") == false)
                {
                    if (System.IO.Directory.CreateDirectory(TempPlaylistPath + "\\RoMoRDuP\\") == null)
                    {
                        MessageBox.Show("Cannot create Directory " + TempPlaylistPath + "\\RoMoRDuP\\");
                        return;
                    }
                }

                if (System.IO.Directory.Exists(TempPlaylistPath + "\\RoMoRDuP\\PlaylistUpdates\\") == true) // empty folder
                {
                    // delete
                    try
                    {
                        System.IO.DirectoryInfo downloadedMessageInfo = new System.IO.DirectoryInfo(TempPlaylistPath + "\\RoMoRDuP\\PlaylistUpdates\\");

                        foreach (System.IO.FileInfo file in downloadedMessageInfo.GetFiles())
                        {
                            file.Delete();
                        }
                        foreach (System.IO.DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
                        {
                            dir.Delete(true);
                        }

                        //System.IO.Directory.Delete(TempPlaylistPath + "\\RoMoRDuP\\PlaylistUpdates\\");
                    }
                    catch (Exception ex)
                    {
                        System.Windows.MessageBox.Show("Can´t delete " + TempPlaylistPath + "\\RoMoRDuP\\PlaylistUpdates\\ ! " + ex.Message);
                    }
                }



                // Generate new Plalist Updates
                int iTempPlaylistIndex = 1;

                foreach (List <string> list in matrixListPlaylists)
                {
                    foreach (string PlaylistFilePath in list)
                    {
                        // load File into RAM
                        StringBuilder sb = new StringBuilder();
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(PlaylistFilePath, Encoding.Default))
                        {
                            String line;
                            // Read and display lines from the file until the end of
                            // the file is reached.
                            while ((line = sr.ReadLine()) != null)
                            {
                                sb.AppendLine(line);
                            }
                        }
                        string strFileListOrgContent = sb.ToString();

                        List <ValidPath> AllValidPaths = GetAllValidPathsFromString(strFileListOrgContent, PlaylistFilePath);

                        {
                            TempPlaylistPath = TempPlaylistPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

                            if (System.IO.Directory.Exists(TempPlaylistPath + "\\RoMoRDuP\\") == false)
                            {
                                if (System.IO.Directory.CreateDirectory(TempPlaylistPath + "\\RoMoRDuP\\") == null)
                                {
                                    MessageBox.Show("Cannot create Directory " + TempPlaylistPath + "\\RoMoRDuP\\");
                                    return;
                                }
                            }

                            if (System.IO.Directory.Exists(TempPlaylistPath + "\\RoMoRDuP\\PlaylistUpdates\\") == false)
                            {
                                if (System.IO.Directory.CreateDirectory(TempPlaylistPath + "\\RoMoRDuP\\PlaylistUpdates\\") == null)
                                {
                                    MessageBox.Show("Cannot create Directory " + TempPlaylistPath + "\\RoMoRDuP\\PlaylistUpdates\\");
                                    return;
                                }
                            }

                            System.IO.FileInfo fileInfo = new System.IO.FileInfo(PlaylistFilePath);
                            TempPlaylistPath = TempPlaylistPath + "\\RoMoRDuP\\PlaylistUpdates\\" + "Temp_" + iTempPlaylistIndex.ToString() + "_" + fileInfo.Name;
                            iTempPlaylistIndex++;
                        }

                        PlayListUpdatesViewModel PlaylistUpdatesEntry = null;

                        foreach (ValidPath validPath in AllValidPaths)
                        {
                            // regenerate all Before Actions already done in UCPartent

                            // check for changes
                            TaskPlanner.FileListEntry changingEntry = null;
                            changingEntry = CheckChangesValidPath(validPath, fileLists.SourceFileListBefore);

                            if (userOptions is UserInterface.UserOptionsMirror) // Only for Mirror
                            {
                                if (changingEntry == null)
                                {
                                    changingEntry = CheckChangesValidPath(validPath, fileLists.TargetFileListBefore);
                                }
                            }

                            // if changes create entry, set bool for create file in AppData/Playlist
                            if (changingEntry != null)
                            {
                                if (PlaylistUpdatesEntry == null)
                                {
                                    PlaylistUpdatesEntry = new PlayListUpdatesViewModel();
                                    PlaylistUpdatesEntry.PathOriginal = PlaylistFilePath;
                                    //PlaylistUpdatesEntry.PathRenameOrg = ;
                                    PlaylistUpdatesEntry.PathTemp = TempPlaylistPath;
                                }

                                PlaylistChange playlistChange = new PlaylistChange();

                                playlistChange.validPath     = validPath;
                                playlistChange.changingEntry = changingEntry;

                                PlaylistUpdatesEntry.PlaylistChanges.Add(playlistChange);
                            }
                        }

                        // create file if changes
                        if (PlaylistUpdatesEntry != null)
                        {
                            if (PlaylistUpdatesEntry.PlaylistChanges.Count > 0)
                            {
                                // check if LeaveCopy/UpdatePlaylist Option was changed before
                                if (old_PlaylistUpdatesItems != null)
                                {
                                    foreach (PlayListUpdatesViewModel oldvm in old_PlaylistUpdatesItems)
                                    {
                                        if (oldvm.PathOriginal == PlaylistUpdatesEntry.PathOriginal)
                                        {
                                            PlaylistUpdatesEntry.LeaveCopy      = oldvm.LeaveCopy;
                                            PlaylistUpdatesEntry.UpdatePlaylist = oldvm.UpdatePlaylist;
                                            break;
                                        }
                                    }
                                }


                                if (userOptions.mainWindow != null)
                                {
                                    userOptions.mainWindow.Dispatcher.Invoke(
                                        (Action)(() =>
                                    {
                                        playlistUpdatesModel.Items.Add(PlaylistUpdatesEntry);
                                        CreateNewPlaylistFile(PlaylistFilePath, strFileListOrgContent, PlaylistUpdatesEntry);
                                    }
                                                 ), null);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }