Example #1
0
        /// <summary>
        /// Pulls the text out of the clipboard and sends it up to the cloud.
        /// </summary>
        /// <exception cref="System.Exception">Unable to Initialize</exception>
        public void CloudClipboardCopy()
        {
            if (!CommonFunctionality.Instance.Initialise())
            {
                throw new Exception("Unable to Initialize");
            }

            CommonFunctionality.Instance.Authenticate(false);

            String ClipboardText = ClipboardHelper.GetClipboardText();



            String filename = ClipboardText.PadRight(CommonFunctionality.CLIPBOARD_PREVIEW_LENGTH, '_') // if the text in the clipboard is too small, make up for it.(add padding):
                              .Substring(0, CommonFunctionality.CLIPBOARD_PREVIEW_LENGTH)               // we make the filename part of the clipboard entry...makes for easy preview access too!
                              + CommonFunctionality.CLIPBOARD_OUT_EXT;


            foreach (char ch in Path.GetInvalidFileNameChars())
            {
                if (!filename.Contains(ch))
                {
                    continue;
                }

                filename = filename.Replace(ch, '_');
            }

            if (File.Exists(filename))
            {
                return;
            }

            // Ensure that this clipboard event we've found ourselves handling is not because the extension swapped out the clipboard file into the vlipboard,
            // tihs would be the case if there is a .in file with the same name as the one we'r eintending to create, dont do this.

            if (File.Exists(Path.Combine(CommonFunctionality.Instance.common_path, Path.GetFileNameWithoutExtension(filename) + CommonFunctionality.CLIPBOARD_IN_EXT)))
            {
                return;
            }


            using (TextWriter tw = File.CreateText(Path.Combine(CommonFunctionality.Instance.common_path, filename)))
            {
                if (!String.IsNullOrEmpty(ClipboardText))
                {
                    tw.Write(ClipboardText);
                }
            }

            // get all pending clipboard files and send them off
            string where = CommonFunctionality.Instance.common_path;
            List <string>        load_clipboard_files = new List <string>(Directory.EnumerateFiles(where));
            bool                 worked     = false;
            IEnumerable <string> enumerable = load_clipboard_files.Where(item => item.EndsWith(CommonFunctionality.CLIPBOARD_OUT_EXT)).AsEnumerable();


            // upload these .co files

            foreach (var filepath in enumerable)
            {
                int retry_count = 3;
                while (!uploadFile(filepath, filename) && retry_count != 0)
                {
                    retry_count--;
                }
                retry_count = 0; //reset
            }
        }