public SetupSplash()
        {
            InitializeComponent();
            this.DescriptionLabel.Size = new System.Drawing.Size(0, 83);

            String descriptionText = "Welcome! This application will consolidate consolidating your pictures into a single location! \n";
            descriptionText += "You'll no longer have to wonder where any pictures are and backups will be a snap!\n";
            descriptionText += "\nBefore we begin, we'll need:\n";
            descriptionText += "     A place to store the pictures - this needs to be a computer with a lot of hard drive space \n";
            descriptionText += "               and the directory needs to be \'shared\' for everyone (sorry, no password is supported yet)\n";
            descriptionText += "               to learn how to share, visit: \n";
            descriptionText += "                http://windows.microsoft.com/en-US/windows7/Share-files-with-someone\n";
            descriptionText += "\n   The directories where you normally keep your pictures. This is a location where you have access and \n";
            descriptionText += "               don\'t have to give administrator rights. It's normally \'My Pictures\' or on your Desktop\n";
            descriptionText += "\nWhen you're ready, click the Start button below!";

            this.DescriptionLabel.Text = descriptionText;
            try
            {
                PixDBInterface pixDb = new PixDBInterface();
                if (!pixDb.CheckIfDbSetup())
                {
                    pixDb.CreateDatabase();
                    pixDb.SetupTables();
                }
            }
            catch (Exception e)
            {
            }
        }
 public ChooseExisting()
 {
     InitializeComponent();
     String descTxt = "If you know the where you'd like to copy the pictures to (the shared folder)\n";
     descTxt += "you can enter it below. For example, a share could be \\\\home-server\\pictures\n";
     descTxt += "\nIf you don't know the location, we can attempt to find it for you.";
     descrTxt.Text = descTxt;
     PixDBInterface pixDb = new PixDBInterface();
     string serverName = pixDb.ReadConfigValue("ServerName");
     if (serverName.Length > 0)
         specifyLocBox.Text = serverName;
 }
        public SelectShareLocation()
        {
            m_LocationSet = false;
            InitializeComponent();

            PixDBInterface pixDb = new PixDBInterface();
            string server = pixDb.ReadConfigValue("ServerName");
            if (string.IsNullOrEmpty(server))
                finishBtn.Enabled = false;
            else
                finishBtn.Enabled = true;

            newShareBtn.Enabled = false;
        }
 private void LoadConfigDirs()
 {
     PixDBInterface pixDb = new PixDBInterface();
     List<string> dirs = pixDb.ReadDirsToWatch(0);
     if (dirs.Count > 0)
     {
         foreach (string entry in dirs)
         {
             ListViewItem curItem = new ListViewItem(entry);
             curItem.Checked = true;
             dirsListView.Items.Add(curItem);
         }
     }
     else
     {
         AddBasicDirs();
     }
 }
        private void doneBtn_Click(object sender, EventArgs e)
        {
            PixDBInterface pixDb = new PixDBInterface();

            String networkShare = null;

            if (networkShareView.CheckedItems.Count > 0)
            {
                ListViewItem item = networkShareView.CheckedItems[0];
                networkShare = item.Text;
            }
            else if (specifyLocBox.Text.Length > 0)
                networkShare = specifyLocBox.Text;

            if (!String.IsNullOrEmpty(networkShare))
            {
                pixDb.UpdateConfigValue("ServerName", networkShare);
                SelectShareLocation.m_LocationSet = true;

                SelectShareLocation loc = new SelectShareLocation();
                loc.Show();
                this.Hide();
            }
        }
        private void nextStepBtn_Click(object sender, EventArgs e)
        {
            if (dirsListView.Items.Count > 0)
            {
                PixDBInterface pixDb = new PixDBInterface();
                pixDb.RemoveAllDirsToWatch();
                foreach (ListViewItem item in dirsListView.Items)
                {
                    string dirToWatch = item.Text;
                    if (dirToWatch.CompareTo("Desktop") == 0)
                        dirToWatch = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                    else if (dirToWatch.CompareTo("My Pictures") == 0)
                        dirToWatch = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

                    pixDb.AddDirToWatch(dirToWatch, 0);
                }
                m_shouldShutdown = false;
                this.Hide();
                SelectShareLocation shareLoc = new SelectShareLocation();
                shareLoc.Show();
            }
        }
 private void AddFileToQueueDatabase(string inFile)
 {
     PixDBInterface dbInter = new PixDBInterface();
     if (!dbInter.CheckIfInPixQueue(inFile))
         dbInter.AddFileToPixQueue(inFile);
 }
 private void finishBtn_Click(object sender, EventArgs e)
 {
     PixDBInterface pixDb = new PixDBInterface();
     pixDb.UpdateConfigValue("ConfigUpdated", "1");
     Application.Exit();
 }
        static void Main(string[] args)
        {
            Logger log = new Logger("Program::main");

            if (args.Length > 0)
            {
                string cmd = args[0];
                if (cmd.CompareTo("shutdown") == 0)
                {
                    PixDBInterface db = new PixDBInterface();
                    if (db.CheckIfDbSetup())
                    {
                        try
                        {
                            // just in case we crashed and the process id in the db is bad
                            string procId = db.ReadConfigValue("ServiceProcessId");
                            int nProcessID = Convert.ToInt32(procId);
                            Process otherProc = Process.GetProcessById(nProcessID);
                            otherProc.Kill();
                        }
                        catch (Exception)
                        { }
                    }
                    return;
                }
            }

            try
            {
                Mutex.OpenExisting("PersonalPictureManagerService");
                log.WriteMessage("Opened the existing mutex");
                return;
            }
            catch (WaitHandleCannotBeOpenedException)
            {
                bool createdNew;
                m_lockingMutex = new Mutex(true, "PersonalPictureManagerService", out createdNew);
                log.WriteMessage("created the mutex");
            }

            try
            {
                PixDBInterface dbConn = new PixDBInterface();
                if (!dbConn.CheckIfDbSetup())
                {
                    log.WriteMessage("setting up database");
                    dbConn.CreateDatabase();
                    dbConn.SetupTables();
                }

                int nProcessID = Process.GetCurrentProcess().Id;
                dbConn.UpdateConfigValue("ServiceProcessId", Convert.ToString(nProcessID));

                String runningVer = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                dbConn.UpdateConfigValue("ServiceProcessVersion", runningVer);

                log.WriteMessage("starting");

                string runningFile = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                string newFile = ConfigManager.GetDataStoreDir() + ConfigManager.AppName;
                log.WriteMessage("newFile: " + newFile);

                if ((File.Exists(newFile)) && (runningFile.CompareTo(newFile) != 0))
                {
                    log.WriteMessage("running: " + newFile);
                    Process.Start(newFile);
                    System.Environment.Exit(0);
                }

                OnConfigTimedEvent(null, null);

                string server = dbConn.ReadConfigValue("ServerName");
                string serverUser = dbConn.ReadConfigValue("ServerLogin");
                string serverPass = dbConn.ReadConfigValue("ServerPass");

                dirList = dbConn.ReadDirsToWatch(0);

                m_fileHelper = new PixFileHelper(server, serverUser, serverPass);
                m_fileHelper.WatchDirs(dirList);

                // update config timer

                aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                // Set the Interval to 5 min
                aTimer.Interval = 300000;
                aTimer.Enabled = true;

                // check website for new rev timer

                configTimer.Elapsed += new ElapsedEventHandler(OnConfigTimedEvent);
                // Set the Interval to 30 min
                configTimer.Interval = 1800000;
                configTimer.Enabled = true;

                // check if anything in the queue

                queueTimer.Elapsed += new ElapsedEventHandler(OnQueueTimedEvent);
                // Set the Interval to 30 min
                queueTimer.Interval = 1800000;
                //queueTimer.Interval = 1000;
                queueTimer.Enabled = true;

                while (true)
                    Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception e)
            {
                System.IO.File.WriteAllText(@"error_log.txt", e.Message);
                System.IO.File.WriteAllText(@"error_log.txt", e.StackTrace);
            }
        }
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            aTimer.Enabled = false;

            Logger log = new Logger("Program::OnTimedEvent");
            try
            {
                PixDBInterface dbConn = new PixDBInterface();

                string configChanged = dbConn.ReadConfigValue("ConfigUpdated");
                if (!string.IsNullOrEmpty(configChanged) && (configChanged.CompareTo("1") == 0))
                {
                    log.WriteMessage("configuration changed");
                    string server = dbConn.ReadConfigValue("ServerName");
                    string serverUser = dbConn.ReadConfigValue("ServerLogin");
                    string serverPass = dbConn.ReadConfigValue("ServerPass");

                    dirList = dbConn.ReadDirsToWatch(0);

                    FileHelper helper = new PixFileHelper(server, serverUser, serverPass);
                    //helper.WatchDirs(dirList);
                    helper.AddDirListener(dirList);
                    dbConn.UpdateConfigValue("ConfigUpdated", "0");
                }
                else
                    log.WriteMessage("no config changed");
            }
            catch (Exception ex)
            {
                log.WriteMessage("Tossed in OnTimedEvent: " + ex.Message);
                log.WriteMessage("stack: " + ex.StackTrace);
            }
            aTimer.Enabled = true;
        }
        private static void OnQueueTimedEvent(object source, ElapsedEventArgs e)
        {
            queueTimer.Enabled = false;
            Logger log = new Logger("Program::OnQueueTimedEvent");
            try
            {
                PixDBInterface dbHelper = new PixDBInterface();

                dbHelper.Test();

                List<string> queuedFiles = dbHelper.GetAllQueuedPix();
                foreach (string file in queuedFiles)
                {
                    if (File.Exists(file))
                    {
                        FileHelper.FileResults addResult = m_fileHelper.AddFile(file);
                        if (addResult == FileHelper.FileResults.ESuccess)
                        {
                            //log.WriteMessage("Wrote file to server: " + file);
                            dbHelper.RemoveEntryFromPixQueue(file);
                        }
                        else if (addResult == FileHelper.FileResults.EAlreadySent)
                        {
                            //log.WriteMessage("File already sent! " + file);
                            dbHelper.RemoveEntryFromPixQueue(file);
                        }
                    }
                    else
                    {
                        //log.WriteMessage("File no longer exists! " + file);
                        dbHelper.RemoveEntryFromPixQueue(file);
                    }
                }
            }
            catch (Exception ex)
            {
                log.WriteMessage("tossed in OnQueueTimedEvent: " + ex.Message);
                log.WriteMessage("stack: " + ex.Message);
            }
            queueTimer.Enabled = true;
        }