Beispiel #1
0
 public void Save()
 {
     try
     {
         if (file != null)
         {
             TextRange range = new TextRange(richTxt.Document.ContentStart,
                                             richTxt.Document.ContentEnd);
             if (range.IsEmpty)
             {
                 CfgPath.MoveToRecycle(file);
             }
             else
             {
                 if (!File.Exists(file))
                 {
                     EnsureDirExist(file);
                 }
                 FileStream fs = new FileStream(file, FileMode.Create);
                 range.Save(fs, System.Windows.DataFormats.Rtf);
                 fs.Close();
             }
         }
     }catch (Exception e)
     {
         Logger.E(e);
         MessageBox.Show(e.Message, "保存文件失败", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
        private void miCopyTagFullPathEx_Click(object sender, RoutedEventArgs e)
        {
            UpdateCurrentTagByContextMenu();
            string dir = CfgPath.GetDirByTag(SelectedTag.Title, true);//拷贝目录名,需要保证路径存在

            dir = System.IO.Path.Combine(dir, DateTime.Now.ToString("yyyyMMdd") + "-");
            ClipBoardSafe.SetText(dir);
        }
        private string[] CopyToHouse(string[] list, string tag)
        {
            List <string> ret     = new List <string>();
            List <string> scrList = new List <string>();
            List <string> dstList = new List <string>();

            foreach (string f in list)
            {
                if (PathHelper.IsValidWebLink(f))
                {
                    ret.Add(f);
                }
                else if (!PathHelper.IsValidFS(f))
                {
                    Logger.E("Copy To House: File not Exist " + f);
                    ret.Add(f);
                }
                else
                {
                    System.Diagnostics.Debug.Assert(PathHelper.IsValidFS(f));
                    FileInfo fi      = new FileInfo(f);
                    string   dstDir  = CfgPath.GetDirByTag(tag, true);//新建文件,保证目录存在
                    string   dstFile = System.IO.Path.Combine(dstDir, fi.Name);
                    if (dstFile == f)
                    {
                        ret.Add(f);
                    }
                    else
                    {
                        ret.Add(dstFile);
                        if (!System.IO.File.Exists(dstFile) && !System.IO.Directory.Exists(dstFile))
                        {
                            scrList.Add(f);
                            dstList.Add(dstFile);
                        }
                    }
                }
            }

            FileShell.SHCopyFile(scrList.ToArray(), dstList.ToArray());
            return(ret.ToArray());

            /*
             * if(dstFile==f)
             * {
             *  return f;
             * }
             * if (!System.IO.File.Exists(dstFile) && !System.IO.Directory.Exists(dstFile))//TODO 已经存在的需要提示覆盖、放弃、重命名
             * {
             *  if(FileShell.CopyFile(f, dstFile))
             *  {
             *      return dstFile;
             *  }
             * }
             * return null;*/
        }
Beispiel #4
0
        public void TestFS_FilesRelocationTest()
        {
            string testFile = @"B:\hello";

            File.Create(testFile).Close();
            FilesRelocationTest(new string[] { CfgPath.GetDirByTag("test", true) + "\\hello" },
                                new string[] { testFile },
                                "test");
            File.Delete(testFile);
        }
Beispiel #5
0
        public void TestFS_TestFileFilter()
        {
            string file = Path.Combine(dir, "1.txt");

            FileStream fs = new FileStream(file, FileMode.CreateNew);

            fs.WriteByte(1);
            Assert.IsFalse(CfgPath.NeedSkip(file));//現在不再檢查該文件是否
            fs.Close();
            Assert.IsFalse(CfgPath.NeedSkip(file));
        }
Beispiel #6
0
 //由于文件变更通知是在一个后台线程中进行的,所以需要通过Invoke机制调用UI主线程中的函数
 private void AddFileToDB_BackThread(string uri)
 {
     if (!CfgPath.NeedSkip(uri))//过滤一些不需要观察的文件
     {
         this.Dispatcher.Invoke(new Action <string>(AddFileToDB_UIThread), uri);
     }
     else
     {
         this.Dispatcher.Invoke(new Action(NotifyList));//有一个bug,当ie保存一个文件时,总是先create,然后delete,然后再次create。
     }
 }
        public void SelectedTagChanged_Callback(GUTag tag)
        {
            SearchBox.Text = tag.Title;
            //现在自己的这个richtextbox非常不好用,将其暂时废除,除非有一个好用的再说
            string uri = CfgPath.GetFileByTag(tag.Title, "note.rtf"); //uri可能不存在

            richTxt.Load(uri);
            ShowUrlListByText();
            //修改text后,会自动触发 TextBox_TextChanged
            //进一步触发             ShowUrlListByText
        }
Beispiel #8
0
        private void ComplateEdit()
        {
            if (!IsVisible)
            {
                return;
            }

            //合法性检查
            string err = CfgPath.CheckTagFormat(Edit.Text);

            if (err != null)
            {
                MessageBox.Show(err, "标签不合法,请重新输入", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }


            //第一步,先将编辑器隐藏起来,否则后面有TextChangedCallback导致业务流程处理后,
            //会有很多意想不到的流程发生,导致多次递归调用本函数(HideEdit)。
            //比如:HideEdit=>lostfocus=>HideEdit
            GUTag  tag       = NoEdit.GUTag;
            string oldTitle  = NoEdit.Text;
            string newTitle  = Edit.Text;
            Canvas parentBak = Parent;



            //将这两个置空,表示该编辑器不可见了(IsVisible = false)。
            Parent = null;
            NoEdit = null;

            try
            {
                //隐藏编辑框
                parentBak.Children.Remove(Edit);
                System.Diagnostics.Debug.WriteLine("HideFloat1");

                //如果文本内容发生修改,通知观察者

                if (oldTitle != newTitle && oldTitle != null && newTitle != null)
                {
                    TextChangedCallback?.Invoke(parentBak, tag, newTitle);
                    System.Diagnostics.Debug.WriteLine("HideFloat2");
                }
            }
            finally
            {
                parentBak?.Focus();   //因为编辑框失去焦点时会有相应的处理,所以这儿设置焦点的处理必须放在所有处理完成之后
                //否则可能形成递归调用
            }
        }
Beispiel #9
0
        public void KeepVDir(string tag)
        {
            //检查功能开关是否关闭,关闭直接返回
            if (!StaticCfg.Ins.Opt.KeepVDir)
            {
                return;
            }

            //一个新的tag,添加在头部
            if (!TagHistory.Contains(tag))
            {
                while (TagHistory.Count >= StaticCfg.Ins.MAX_TAG_VDIR)
                {
                    TagHistory.RemoveAt(0);
                }
                TagHistory.Add(tag);
            }
            //一个已有的tag,换一下位置
            else
            {
                TagHistory.Remove(tag);
                TagHistory.Add(tag);
            }

            //新建虚拟目录
            foreach (string t in TagHistory)
            {
                string tagVDir = CfgPath.GetVDirByTag(t);
                string tagDir  = CfgPath.GetDirByTag(t);//目前这个功能已经废除了,但不需要在这儿新建目录
                if (Directory.Exists(tagDir))
                {
                    PathHelper.LinkDir(tagVDir, tagDir);
                }
            }

            //如果溢出的话,淘汰老的目录
            DirectoryInfo vroot = new DirectoryInfo(CfgPath.VDir);

            DirectoryInfo[] vdirs     = vroot.GetDirectories();
            int             vDirCount = vdirs.Length;

            foreach (DirectoryInfo v in vdirs)
            {
                if (!TagHistory.Contains(v.Name) && vDirCount > StaticCfg.Ins.MAX_TAG_VDIR)
                {
                    v.Delete();
                    vDirCount--;
                }
            }
        }
Beispiel #10
0
        //编辑框失去焦点,表示输入结束,关闭编辑框并通知变更
        private void Edit_LostFocus(object sender, RoutedEventArgs e)
        {
            string err = CfgPath.CheckTagFormat(Edit.Text);

            if (err != null)
            {
                CancelEdit();
                MessageBox.Show(err, "标签不合法,请重新输入", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else
            {
                ComplateEdit();
            }
        }
        //公有成员方法************************************************************
        public static List <SearchResultItem> QueryByTag(string tag, IUriDB db)
        {
            List <string>           files = db.Query(tag);
            List <SearchResultItem> ret   = new List <SearchResultItem>();

            foreach (string uri in files)
            {
                string formatUri = CfgPath.ChangePathRoot(uri);
                if (PathHelper.IsValidUri(formatUri))
                {
                    SearchResultItem it = new SearchResultItem();
                    it.Init(uri, formatUri, db);
                    ret.Add(it);
                }
            }
            return(ret);
        }
        private void EditFile(string dotPostfix)
        {
            UpdateCurrentTagByContextMenu();
            if (SelectedTag == null)
            {
                return;
            }

            string defaultFile = CfgPath.GetTemplateFileByTag(SelectedTag.Title, dotPostfix);

            if (defaultFile == null)
            {
                return;
            }

            FileShell.OpenFile(defaultFile);
        }
Beispiel #13
0
 private void Dbg()
 {
     if (File.Exists(@"B:\DbgTagExplorer=1"))
     {
         string dir = @"B:\TagExplorer";
         if (Directory.Exists(dir) && MessageBox.Show(dir, "Del", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
         {
             Directory.Delete(dir, true);
         }
         if (!Directory.Exists(dir))
         {
             Directory.CreateDirectory(dir);
         }
         CfgPath.SetRootPathNoSave(dir);
         GitHelper h = new GitHelper();
         h.Clone();
     }
 }
Beispiel #14
0
        private static void MoveToRecycle(IEnumerable <string> Uris)
        {
            List <string> srcs = new List <string>();

            srcs.AddRange(Uris);
            List <string> dstList = new List <string>();
            List <string> srcList = new List <string>();

            foreach (string s in srcs)
            {
                if (PathHelper.IsValidFS(s))
                {
                    string dst = CfgPath.GetRecycleByPath(s);
                    dstList.Add(dst);
                    srcList.Add(s);
                }
            }
            FileShell.SHMoveFiles(srcList.ToArray(), dstList.ToArray());
        }
Beispiel #15
0
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="file"></param>
        public static void OpenFile(string file)
        {
            if (file == null)
            {
                return;
            }
            try
            {
                if (IsNoteFile(file))
                {
                    string wordpad = CfgPath.GetWordPadExeFile();
                    Process.Start(wordpad, file);
                    return;
                }

                Logger.D("StartFile {0} ", file);
                if (!PathHelper.IsValidUri(file))
                {
                    Logger.E("Start File ERROR,File isn't valid!~");
                    return;
                }

                UpdateAccessTime(file);
                if (File.Exists(file))
                {
                    //之所以搞下面这么复杂的流程,是因为zte的一个文档安全软件导致Process.Start(file);报错
                    //最终原因貌似与观察文件变更有关系,现在在文件变更过程通过定时器来触发,似乎就没有问题了。
                    Logger.D("StartFile {0} is valid", file);

                    Process p = Process.Start(file);

                    return;
                }
                else
                {
                    Process.Start(file);
                }
            }
            catch (Exception ee)
            {
                Logger.E(ee);
            }
        }
Beispiel #16
0
        //UI主线程中的方法调用
        private void AddFileToDB_UIThread(string uri)
        {
            Logger.I("AddFileInDoc={0}", uri);
            string tag = CfgPath.GetTagByPath(uri);

            if (tag != null)
            {
                UriDB.AddUris(new List <string>()
                {
                    uri
                }, new List <string>()
                {
                    tag
                });
            }
            else
            {
                Logger.E("观察到文件变化,但该文件不在文件仓库中=={0}", uri);
            }
        }
Beispiel #17
0
        private void App_Startup(object sender, StartupEventArgs e)
        {
            TipsCenter.Ins.StartTime = "App:117";
            if (e.Args.Length > 0)
            {
                CfgPath.SetRootPathNoSave(e.Args[0]);
            }
            Dbg();

retry:
            if (CfgPath.RootPath == null)
            {
                System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
                fd.ShowNewFolderButton = true;
                fd.ShowDialog();
                if (Directory.Exists(fd.SelectedPath))
                {
                    CfgPath.RootPath = fd.SelectedPath;
                }
                else
                {
                    if (System.Windows.MessageBox.Show("请选择一个文档存储的根目录", "首次运行设置", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
                    {
                        goto retry;
                    }
                    else
                    {
                        System.Windows.Application.Current.Shutdown();
                    }
                }
            }


            Process process = GetRuningInstance();

            if (process != null)
            {
                BringToForeground(process);
                Environment.Exit(0);
            }
        }
Beispiel #18
0
        public GUTag QueryTagByPath(string path)
        {
            if (qPathCmd == null)
            {
                qPathCmd = new SQLiteCommand(@"SELECT * FROM Tags where (Path=@Path)", Conn);
                qPathCmd.Parameters.AddRange(new[] {
                    new SQLiteParameter("@Path", DbType.String),
                });
            }

            List <GUTag> ret = new List <GUTag>();

            qPathCmd.Parameters[0].Value = path;
            using (SQLiteDataReader r = qPathCmd.ExecuteReader())
            {
                while (r.Read())
                {
                    GUTag tag = ReadGUTagFromR(r);
                    ret.Add(tag);
                }
            }
            if (ret.Count > 0)
            {
                return(ret[0]);
            }
            else
            {
                string       tagTitle = CfgPath.GetTagByPath(path);
                List <GUTag> tags     = QueryTags(tagTitle);
                foreach (var tag in tags)
                {
                    if (tag.GetDir() == path)
                    {
                        return(tag);
                    }
                }
            }
            return(null);
        }
        //private void scrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
        //{
        //    double h = e.NewSize.Height;


        //    //canvasMinHeight = this.ActualHeight - 60;
        //    //SetHeight();

        //    System.Diagnostics.Debug.WriteLine(e.NewSize.Height+" "+e.NewSize.Width + " "+rootTag + " "+currentTag);
        //    RedrawGraph();
        //}

        private void miNewFile_Click(object sender, RoutedEventArgs e)
        {
            UpdateCurrentTagByContextMenu();
            string         initDir = CfgPath.GetDirByTag(SelectedTag.Title, true);//新建文件,保证目录存在
            SaveFileDialog sf      = new SaveFileDialog();

            sf.InitialDirectory = initDir;

            sf.Filter = TemplateHelper.GetTemplateFileFilter();//"One文件(*.one)|*.one|Mind文件(*.xmind)|*.xmind";
            if (sf.ShowDialog() == true)
            {
                if (File.Exists(sf.FileName))
                {
                    MessageBox.Show("该文件已经存在" + sf.FileName, "文件名冲突", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                else
                {
                    FileInfo fi          = new FileInfo(sf.FileName);
                    string   tmplateFile = TemplateHelper.GetTemplateByExtension(fi.Extension);
                    if (tmplateFile != null && File.Exists(tmplateFile))
                    {
                        File.Copy(tmplateFile, sf.FileName);
                        AddUri(new List <string>()
                        {
                            sf.FileName
                        });
                        FileShell.OpenFile(sf.FileName);
                    }
                    else
                    {
                        File.Create(sf.FileName).Close();
                    }
                }
            }
        }
 private void miCopyTagFullPath_Click(object sender, RoutedEventArgs e)
 {
     UpdateCurrentTagByContextMenu();
     ClipBoardSafe.SetText(CfgPath.GetDirByTag(SelectedTag.Title, true)); //拷贝完整路径名,需要保证目录存在
 }
Beispiel #21
0
 private static void AssertFilter(bool r, string s)
 {
     Assert.AreEqual(r, CfgPath.NeedSkip(s));
 }
Beispiel #22
0
 private static void AssertTag(string file, string tag)
 {
     //createdir(file);
     Assert.AreEqual(tag, CfgPath.GetTagByPath(file));
 }