Example #1
0
        protected task elab_task_folder(synch_results res, int synch_folder_id, string folder_path, DateTime ct, DateTime lwt, long folder_id)
        {
            task t = task.parse_task(core, synch_folder_id, folder_path, ct, lwt, _users, _labels, folder_id: folder_id, set_index: true);

            if (t != null)
            {
                // rinomino il folder
                string folder_name = Path.GetFileName(folder_path);
                if (folder_name.ToLower() != (t.title + ".task").ToLower())
                {
                    try {
                        string dn = Path.GetDirectoryName(folder_path), new_name = t.title + ".task";
                        t.path = Path.Combine(dn, new_name);
                        if (Directory.Exists(t.path))
                        {
                            throw new Exception("cè già la cartella " + t.path);
                        }
                        Directory.Move(folder_path, t.path);
                        fire_synch_event("rinominato folder: " + folder_path + ", in: " + Path.Combine(dn, new_name));
                        folder_name = new_name; folder_path = Path.Combine(dn, new_name);
                        update_folder_name_db(folder_name, folder_id);
                    } catch (Exception ex) { log.log_err(ex); res.err = ex.Message; }
                }

                // aggiorno l'indice
                DateTime?lwt_i = t.doc != null ? t.doc.lwt : (DateTime?)null;
                if (t.doc != null && t.doc.changed)
                {
                    t.doc.save_into_folder(core, folder_path);
                    fire_synch_event("salvato index doc: " + t.doc.path);
                    lwt_i = t.doc.lwt;
                }

                t.id = set_task_db(t, out string tp, out int cc, lwt_i);
                if (tp == "insert")
                {
                    fire_synch_event("aggiunto task al database: " + t.title);
                }
                else if (tp == "update" && cc > 0)
                {
                    fire_synch_event("aggiornato task nel database: " + t.title);
                }
            }
            return(t);
        }
Example #2
0
        protected synch_results init_synch_folder(int synch_folder_id, string path
                                                  , long?parent_id = null, task parent_task = null, synch_results res = null, bool check = false)
        {
            if (res == null)
            {
                res = new synch_results();
            }
            try {
                // folders
                foreach (string fp in Directory.EnumerateDirectories(path))
                {
                    DirectoryInfo di = new DirectoryInfo(fp);
                    DateTime      ct = sys.without_ms(di.CreationTime), lwt = sys.without_ms(di.LastWriteTime);
                    string        folder_name = di.Name;

                    long folder_id = 0; task t = null;
                    if (!check)
                    {
                        // folder
                        string tp; int cc = 0;
                        folder_id = set_folder_db(synch_folder_id, parent_id, folder_name, ct, lwt, out tp, out cc);
                        if (tp == "insert")
                        {
                            fire_synch_event("aggiunto folder al database: " + fp);
                        }
                        else if (tp == "update" && cc > 0)
                        {
                            fire_synch_event("aggiornato folder nel database: " + fp);
                        }

                        // task folder
                        if (parent_task == null)
                        {
                            t = elab_task_folder(res, synch_folder_id, fp, ct, lwt, folder_id);
                            if (t != null)
                            {
                                di = new DirectoryInfo(t.path); folder_name = di.Name;
                            }
                        }
                    }

                    res.folders++;
                    if (parent_task != null)
                    {
                        parent_task.level_folder++;
                    }
                    res = init_synch_folder(synch_folder_id, Path.Combine(path, folder_name), folder_id, t != null ? t : parent_task, res, check);
                }

                // files
                string i_task = core.config.get_var("lib-vars.index-folder").value;
                foreach (string fn in Directory.EnumerateFiles(path))
                {
                    FileInfo fi = new FileInfo(fn);
                    DateTime ct = sys.without_ms(fi.CreationTime), lwt = sys.without_ms(fi.LastWriteTime);
                    if (lwt > res.lwt)
                    {
                        res.lwt = lwt;
                    }

                    // web.config
                    if (fi.Name.ToLower() == "web.config" && !parent_id.HasValue)
                    {
                        continue;
                    }

                    // __i.xml
                    if (parent_task != null && fi.Name == i_task)
                    {
                        continue;
                    }

                    if (!check)
                    {
                        // file
                        long file_id = set_file_db(synch_folder_id, parent_id, fi.Name, fi.Extension, ct, lwt, out string tp, out int cc);
                        if (tp == "insert")
                        {
                            fire_synch_event("aggiunto file al database: " + fn);
                        }
                        else if (tp == "update" && cc > 0)
                        {
                            fire_synch_event("aggiornato file nel database: " + fn);
                        }

                        // file content
                        string    new_content = "";
                        file_info info        = is_info_file(fi.Name);
                        file_type ftp         = is_type_file(fi.Extension);
                        if (info != null || ftp != null)
                        {
                            if (tp == "insert" || (tp == "update" && cc > 0))
                            {
                                new_content = File.ReadAllText(fn);
                                if (new_content.Replace(" ", "").Replace("\r", "").Replace("\n", "") != "")
                                {
                                    set_file_content_db((int)file_id, Path.GetExtension(fn).ToLower(), new_content, ct, lwt);
                                    fire_synch_event("salvato contenuto file nel database: " + fn);

                                    if (parent_task != null && info != null)
                                    {
                                        if (set_task_notes_db(parent_task.id, file_id, new_content, file_type.ft_type_content.info, ct, lwt))
                                        {
                                            fire_synch_event($"salvate le note del task nel database: {fn}");
                                        }
                                    }
                                }
                            }
                        }

                        // task file
                        if (parent_task == null)
                        {
                            task t = task.parse_task(core, synch_folder_id, fn, ct, lwt, _users, _labels, file_id: file_id);
                            if (t != null)
                            {
                                long task_id = set_task_db(t, out tp, out cc);
                                if (tp == "insert")
                                {
                                    fire_synch_event("inserito task nel database: " + Path.Combine(path, t.title));
                                }
                                else if (tp == "update" && cc > 0)
                                {
                                    fire_synch_event("aggiornato il task nel database: " + Path.Combine(path, t.title));
                                }

                                // task notes
                                string notes = "";
                                if (ftp != null && (tp == "insert" || (tp == "update" && cc > 0)))
                                {
                                    if (set_task_notes_db(task_id, file_id, new_content, ftp.type_content, ct, lwt, out notes))
                                    {
                                        fire_synch_event($"salvate le note del task nel database: {fn}");
                                    }
                                }

                                // file -> folder
                                long folder_id = task_file_to_folder(synch_folder_id, (int)task_id, t.title, fn, (int)file_id, parent_id, ct, out string new_folder_path, notes);

                                // reparse folder task
                                elab_task_folder(res, synch_folder_id, new_folder_path, ct, lwt, folder_id);
                            }
                        }
                    }

                    res.files++;
                }

                // task folder - dt_upd
                if (parent_task != null && !check)
                {
                    string cc = db_conn.exec(core.parse_query("lib-notes.upd-task-date", new string[, ] {
                        { "task_id", parent_task.id.ToString() }
                    }));
                    if (cc != "0")
                    {
                        fire_synch_event("aggiornato task date nel database: " + Path.Combine(Path.GetDirectoryName(path), parent_task.title));
                    }
                }
            } catch (Exception ex) { log.log_err(ex.Message); res.err = ex.Message; }
            return(res);
        }
Example #3
0
        public synch_results reload_folders(bool check = false, bool force = false)
        {
            reload_settings();

            synch_results res = new synch_results();

            try {
                // check cache
                synch_results rc = null;
                if (!check)
                {
                    res.scan = true;

                    // seconds
                    if (!force)
                    {
                        string   prev_last = get_cache_var("synch-last", -1);
                        DateTime last      = prev_last != "" ? DateTime.Parse(prev_last) : DateTime.MinValue;
                        setting  s         = settings.get_setting(core, db_conn, "synch-seconds");
                        if (s != null && last != DateTime.MinValue && (DateTime.Now - last).TotalSeconds < int.Parse(s.value))
                        {
                            res.scan = false;
                            return(res);
                        }
                    }

                    // files, folders, lwt
                    int prev_files    = int.Parse(get_cache_var("synch-files", -1, "-1"))
                    , prev_folders    = int.Parse(get_cache_var("synch-folders", -1, "-1"));
                    string   prev_lwt = get_cache_var("synch-lwt", -1);
                    DateTime lwt      = prev_lwt != "" ? DateTime.Parse(prev_lwt) : DateTime.MinValue;
                    rc = reload_folders(true);
                    if (!force && ((prev_files > 0 && prev_files == rc.files) &&
                                   (prev_folders > 0 && prev_folders == rc.folders) &&
                                   (lwt != DateTime.MinValue && lwt == rc.lwt)))
                    {
                        set_cache_var("synch-last", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), -1);
                        res.scan = false;
                        return(res);
                    }
                }

                DateTime start = DateTime.Now;
                if (!check)
                {
                    fire_synch_event("elenco cartelle da sincronizzare", true);
                }

                // folders to synch
                this.synch_folders = list_synch_folders(Environment.MachineName);
                if (!check)
                {
                    foreach (synch_folder f in this.synch_folders)
                    {
                        fire_synch_event(string.Format("   - cartella di sincronizzazione '{0}' - {1}, path: {2}"
                                                       , f.title, f.des, f.local_path), true);
                    }
                }

                // leggo le cartelle
                if (!check)
                {
                    clean_readed();
                }
                foreach (synch_folder f in this.synch_folders)
                {
                    if (!check)
                    {
                        fire_synch_event($"elaboro la cartella {f.local_path}...", true);
                    }
                    res = init_synch_folder(f.id, f.local_path, res: res, check: check);
                }

                if (!check)
                {
                    del_unreaded(out int cc_files, out int cc_folders);
                    res.deleted = cc_files + cc_folders;
                    if (res.deleted > 0)
                    {
                        if (cc_files > 0)
                        {
                            fire_synch_event($"cancellati dal database: {cc_files} files");
                        }
                        if (cc_folders > 0)
                        {
                            fire_synch_event($"cancellati dal database: {cc_folders} folders");
                        }
                    }
                }

                res.seconds = (int)(DateTime.Now - start).TotalSeconds;

                // check cache
                if (!check)
                {
                    set_cache_var("synch-files", rc.files.ToString(), -1);
                    set_cache_var("synch-folders", rc.folders.ToString(), -1);
                    set_cache_var("synch-lwt", rc.lwt.ToString("yyyy/MM/dd HH:mm:ss"), -1);
                    set_cache_var("synch-last", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), -1);
                }
            } catch (Exception ex) { res.err = ex.Message; log.log_err(ex.Message); } finally { }

            return(res);
        }