public Watcher(ref WeeboxSync weebox, string path)
        {
            this.path = path;
            this.weebox = weebox;

            // Create a new FileSystemWatcher and set its properties.
            watcher = new FileSystemWatcher {
                                                Path = path,
                                                NotifyFilter = NotifyFilters.FileName | NotifyFilters.Attributes | NotifyFilters.Size
                                            };
            // Add event handlers.
            watcher.Changed += OnChanged;
            watcher.Created += OnChanged;
            watcher.Deleted += OnChanged;
            watcher.Renamed += OnChanged;

            watcher.IncludeSubdirectories = true;
            Enable ();
        }
 public TrayApp(ref WeeboxSync instance)
 {
     weebox = instance;
     trayMenu = new ContextMenu();
     var menu = trayMenu.MenuItems;
     MenuItem m = new MenuItem("Forçar sincronização", ForceSync);
     menu.Add(0, m);
     m = new MenuItem("Alterar credenciais", SetUserCredentials);
     menu.Add (1, m);
     m = new MenuItem("Alterar dados de ligação", SetConnectionInfo);
     menu.Add(2, m);
     m = new MenuItem("Sair", OnExit);
     menu.Add (3, m);
     trayIcon = new NotifyIcon {
                                   Text = "Weebox-Sync",
                                   Icon =
                                       new Icon(@"Icons\weebox_tray_icon2.ico"),
                                   ContextMenu = trayMenu,
                                   Visible = true
     };
 }
 public LoginWindow(ref WeeboxSync weeboxInstance)
 {
     InitializeComponent();
     this.weebox = weeboxInstance;
 }
 public DownloadWait(ref WeeboxSync weeboxInst)
 {
     InitializeComponent();
     weebox = weeboxInst;
 }
        public static void setup()
        {
            ConnectionInfo con = new ConnectionInfo(
                new Utilizador("g10_demo", "demo"),
                "http://photo.weebox.keep.pt/", "http://proxy.uminho.pt/");

            CoreAbstraction core = CoreAbstraction.getCore();

            WeeboxSync wee = new WeeboxSync();
            FicheiroSystemAbstraction abs = new FicheiroSystemAbstraction();
            DataBaseAbstraction ab = new DataBaseAbstraction();
            ab.deleteALl();
            if (Directory.Exists(@"c:\users\fabiim\my documents\my weebox")){
                abs.DeleteRecursiveFolder(@"c:\users\fabiim\my documents\my weebox");
            }
            wee.connection_info = con;
            wee.setDefaultRootFolder();
            Watcher w= new Watcher(ref wee, wee.getRootFolder() + "\\Bundles");
            wee.SetWatcher(ref w);
               w.Disable();
               while (true){
               try{
                   wee.setup();
                   while (true){
                       wee.SynchronizeAll();
                   }
               }
               catch (Exception e){
                   Console.Out.Write("Exception");
                   Console.Out.Write(e);
               }
               }
            try{
                wee.setup();

                while (true){
                    wee.SynchronizeAll();
                }
            }catch (Exception e){
                Console.Out.Write("Exception");
                Console.Out.Write(e);
                Console.Read();

            }
            /*            //wee.getBundleInfo("2BF883C16A5D8C2E827CD47A6EA19028");
            Ficheiro file = new Ficheiro(@"c:\asd.png", true);
            List<Ficheiro> files = new List<Ficheiro>(){ new Ficheiro(@"c:\asd.png", true), new Ficheiro(@"c:\drf.png", true) };
            wee.PutFicheiros("689421950C2542A0A7E09C76E40536C4", files);
            */
            //wee.RemoveFicheiros(
            //            wee.RemoveFicheiros("E37ADFC66E0ECB631998DFB13B34BB63",
            //                                new List<string>()
            //                                    {
                                    //    "1F8FEC6875461D0B5BA6EA7972486F8B",
            //                                        "66BF8E7E5DCAF1D96FEE3EA78D8549CD"
              //                                  });

            Console.ReadLine();
            //            wee.PutFicheiro("", new Ficheiro());

            //con.user
            //con.proxy.ToString();

            //            WeeboxSync weebox = new WeeboxSync();
            //          weebox.connection_info = con;
            //        weebox.setup();
            //            CoreAbstraction core = CoreAbstraction.getCore();
            //            core.SetConnection(con);

            //            core.PutFicheiro("", new Ficheiro());
        }
        public static void Main()
        {
            #region main
            _setupOk = true;
            if (IsFirstRun())
            {
                _setupOk = DoSetup();
                _defaultInterval = Weebox.DefaultSyncInterval;
            }
            else {
                //iniciar nova instância
                Weebox = new WeeboxSync ();
                DataBaseAbstraction dba = new DataBaseAbstraction ();
                ConnectionInfo ci = null;
                try{
                    GetRegistryKeys();
                    ci = dba.GetConnectionInfo (_username);
                } catch (Exception e) {
                    MessageBox.Show("Erro ao recuperar credenciais de utilizador.", "Erro");
                    return;
                }
                Weebox.connection_info = ci;
                Weebox.saveRootFolder (_rootFolder);
                Weebox.DefaultSyncInterval = _defaultInterval;

            }//end else

            if (!_setupOk) {
                return;
            }

            //Create watcher
            Thread t = new Thread(StartWatcher) { IsBackground = true };
            t.Start();

            //create sync timer thread
            Thread timer = new Thread (CheckSync) {IsBackground = true};
            //if the parent thread terminates, all the background threads terminate too
            timer.Start();

            //Create the tray icon
            TrayApp ta = new TrayApp(ref Weebox);
            Application.Run(ta);

            #endregion

            #region tests

            /*
            string wid = "tag_id_1";

            Tag t0 = new Tag("p", "/p", "tag_id_0");
            Tag t1 = new Tag("t1", "/p/t1", "tag_id_1");
            Tag t2 = new Tag("t2", "/p/t1/t2", "tag_id_2");
            Tag t3 = new Tag("t3", "/p/t1/t2/t3", "tag_id_3");
            Tag t4 = new Tag("t4", "/p/t1/t2/t4", "tag_id_4");
            Tag t5 = new Tag("t5", "/p/t1/t2/t5", "tag_id_5");
            Tag t6 = new Tag("t6", "/p/t1/t6", "tag_id_6");
            Tag t7 = new Tag("t7", "/p/t1/t6/t7", "tag_id_7");
            Tag t8 = new Tag("t8", "/p/t1/t6/t8", "tag_id_8");
            Tag t9 = new Tag("t9", "/p/t9", "tag_id_9");
            Tag t10 = new Tag("t10", "/p/t9/t10", "tag_id_10");
            Tag t11 = new Tag("t11", "/p/t9/t11", "tag_id_11");
            Tag t12 = new Tag("t12", "/p/t9/t12", "tag_id_12");
            Tag t13 = new Tag("t13", "/p/t13", "tag_id_13");
            Tag t14 = new Tag("t14", "/p/t13/t14", "tag_id_14");
            Tag t15 = new Tag("t14", "/p/t13/t14/t15", "tag_id_15");
            Tag t16 = new Tag("t14", "/p/t13/t14/t15/t16", "tag_id_16");
            Tag t17 = new Tag("t14", "/p/t13/t14/t15/t16/t17", "tag_id_17");
            Tag t18 = new Tag("t14", "/p/t13/t14/t15/t16/t17/t18", "tag_id_18");

            Scheme sc = new Scheme ("scheme_0", t0);
            sc.arvore.add (t1, t0.Path, t1.Path);
            sc.arvore.add(t2, t1.Path, t2.Path);
            sc.arvore.add(t3, t2.Path, t3.Path);
            sc.arvore.add(t4, t3.Path, t4.Path);
            sc.arvore.add(t5, t4.Path, t5.Path);
            sc.arvore.add(t6, t5.Path, t6.Path);
            sc.arvore.add(t7, t6.Path, t7.Path);
            sc.arvore.add(t8, t7.Path, t8.Path);
            sc.arvore.add(t9, t8.Path, t9.Path);
            sc.arvore.add(t10, t9.Path, t10.Path);
            sc.arvore.add(t11, t10.Path, t11.Path);
            sc.arvore.add(t12, t11.Path, t12.Path);
            sc.arvore.add(t13, t12.Path, t13.Path);
            sc.arvore.add(t14, t13.Path, t14.Path);
            sc.arvore.add(t15, t14.Path, t15.Path);
            sc.arvore.add(t16, t15.Path, t16.Path);
            sc.arvore.add(t17, t16.Path, t17.Path);
            sc.arvore.add(t18, t17.Path, t18.Path);

            Tag t110 = new Tag("t", "/t", "tag_id_110");
            Tag t111 = new Tag("t111", "/t/111", "tag_id_111");
            Tag t112 = new Tag("t112", "/t/112", "tag_id_112");
            Tag t113 = new Tag("t113", "/t/111/113", "tag_id_113");
            Tag t114 = new Tag("t114", "/t/111/114", "tag_id_114");
            Tag t115 = new Tag("t115", "/t/112/115", "tag_id_115");

            Scheme sc1 = new Scheme ("scheme_2", t110);
            sc1.arvore.add(t111, t110.Path, t111.Path);
            sc1.arvore.add(t112, t111.Path, t112.Path);
            sc1.arvore.add(t113, t112.Path, t113.Path);
            sc1.arvore.add(t114, t113.Path, t114.Path);
            sc1.arvore.add(t115, t114.Path, t115.Path);

            List<Scheme> lista = new List<Scheme> (2);
            lista.Add (sc);
            lista.Add (sc1);

            dba.SaveClassificationScheme (lista);

            lista = null;
            lista = dba.GetClassificationScheme ();

            Console.WriteLine("breakpoint");

            string bid = "id1";

            List<Ficheiro> l = new List<Ficheiro> ();
            for(int i = 0; i<40; i++) {
                Ficheiro f = new Ficheiro("path_"+ i, bid, "md5_"+i);
                l.Add (f);
            }

            Bundle bd = new Bundle {filesPath = l, localId = bid, meta = null, weeId = bid, weeTags = null};

            dba.SaveBundle (bd);

            dba.DeleteBundle (bd.localId);

            Console.WriteLine("breakpoint");
            */
            /*
            Bundle received = dba.getBundle (bid);

            IList<Ficheiro> listFicheiros = dba.GetFicheirosIDS (bid);

            #endregion
            }

            private static void StartWatcher()
            {
            Watcher w = new Watcher(ref Weebox, Weebox.getRootFolder () + @"\Bundles");
            Weebox.SetWatcher (ref w);
            }

            private static void GetRegistryKeys() {
            _rootFolder = (string) Microsoft.Win32.Registry.GetValue(
                /*key*/    @"HKEY_CURRENT_USER\Software\KeepSolutions\Weebox",
                /*value*/    "rootFolder",
                /*default return value*/    "");
            _username = (string) Microsoft.Win32.Registry.GetValue(
                /*key*/    @"HKEY_CURRENT_USER\Software\KeepSolutions\Weebox",
                /*value*/    "username",
                /*default return value*/    "");
            _defaultInterval = (int) Microsoft.Win32.Registry.GetValue(
                /*key*/    @"HKEY_CURRENT_USER\Software\KeepSolutions\Weebox",
                /*value*/    "syncInterval",
                /*default return value*/    0);
        }
        private static bool DoSetup()
        {
            ConnectionInfo ci = null;
            Weebox = new WeeboxSync();
            int state = 1;
            bool cont = true;
            FicheiroSystemAbstraction fsa = new FicheiroSystemAbstraction();
            WelcomeScreen ws = new WelcomeScreen ();
            ws.ShowDialog ();
            do {
                /**
                * if dialog results are DialogResult.Retry,
                * show the last form (back button was pressed)
                * if the result is Cancel, setup is to be canceled entirely
                * if the result is OK, follow to the next form, up until download.
                */

                DialogResult result;
                switch (state) {
                    case 1:
                        ci = new ConnectionInfo();
                        ConnectionInfoEditor cie = new ConnectionInfoEditor (ref ci);
                        result = cie.ShowDialog ();
                        if (result == DialogResult.Cancel)
                            return false;
                        if(result == DialogResult.Retry)
                            state = 0; // exit
                        else
                            state++;
                        break;
                    case 2:
                        if(ci == null) {
                            state = 0;
                            break;
                        }
                        Weebox.connection_info = ci;
                        LoginWindow lw = new LoginWindow ( ref Weebox);
                        result = lw.ShowDialog ();
                        if (result == DialogResult.Cancel)
                            return false;
                        if(result == DialogResult.Retry)
                            state--; // back to previous dialog
                        else
                            state++; // next dialog
                        break;
                    case 3:
                        FolderChooser fc = new FolderChooser (ref Weebox);
                        result = fc.ShowDialog ();
                        if (result == DialogResult.Cancel)
                            return false;
                        if (result == DialogResult.Retry)
                            state--; // back to previous dialog
                        else
                            state++; // next dialog
                        break;
                    case 4:
                        //setup successful, go to download
                        SaveRegistryKeys ();
                        DataBaseAbstraction dba = new DataBaseAbstraction ();
                        try
                        {
                            dba.SaveConnectionInfo(Weebox.connection_info);
                        }
                        catch (Exception e) {
                            //eliminar chaves de registo
                            DeleteRegistryKeys();
                            //eliminar pasta root
                            fsa.DeleteRecursiveFolder(Weebox.getRootFolder());
                            MessageBox.Show(
                                "Setup falhou. Por favor inicie novamente a aplicação, verifique os dados inseridos e tente novamente",
                                "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return false;
                        }
                        DownloadWait dw = new DownloadWait (ref Weebox);
                        var res = dw.ShowDialog ();
                        if(res == DialogResult.Cancel) {
                            //eliminar chaves de registo
                            DeleteRegistryKeys();
                            //eliminar pasta root
                            fsa.DeleteRecursiveFolder(Weebox.getRootFolder());
                            MessageBox.Show(
                                "Setup falhou. Por favor inicie novamente a aplicação, verifique os dados inseridos e tente novamente",
                                "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return false;
                        }
                        else if (res == DialogResult.OK) {
                            MessageBox.Show ("O setup foi concluído com sucesso.\nBem-vindo ao mundo Weebox.", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        cont = false;
                        break;

                    case 5:
                        // all tasks done, exit

                        return true;
                    case 0:
                        //Cancel setup and exit application
                        Application.Exit ();
                        return false;
                    default:
                        MessageBox.Show (
                            "Houve um erro no processo de instalação.\n" +
                            "Por favor volte a iniciar a aplicação para concluir o processo,",
                            "Erro",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Error);
                        //eliminar chaves de registo
                        DeleteRegistryKeys();
                        //eliminar pasta root
                        fsa.DeleteRecursiveFolder(Weebox.getRootFolder());
                        Application.Exit();
                        break;
                }
            } while (cont);
            //MessageBox.Show ("Setup done!, Now we rock!");
            return true;
        }
 public FolderChooser(ref WeeboxSync weeboxInstance)
 {
     InitializeComponent();
     weebox = weeboxInstance;
     rootFolderTextBox.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
 }
 private void SetUserCredentials(object sender, EventArgs eventArgs)
 {
     WeeboxSync ws = new WeeboxSync ();
     LoginWindow lw = new LoginWindow(ref ws);
     var dialogRes = lw.ShowDialog();
     if (dialogRes == DialogResult.OK) { //nao foi cancelado
         var res = weebox.setCredentials (ws.connection_info.user.user, ws.connection_info.user.pass);
         if (!res) {
             MessageBox.Show ("Sincronização em curso, por favor aguarde alguns minutos e tente novamente.",
                              "Erro",
                              MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }