Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

ClipboardFusion Item Exporter

Description
This macro exports the selected clipboard items to a file that you can later import back into ClipboardFusion using the "ClipboardFusion Item Importer" macro.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
May 30, 2018
Date Last Modified
May 30, 2018

Macro Code

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Text;
using System.Drawing.Imaging;

// The 'text' parameter will contain the text from the:
//   - Current Clipboard when run by HotKey
//   - History Item when run from the History Menu
// The returned string will be:
//   - Placed directly on the Clipboard
//   - Ignored by ClipboardFusion if it is 'null'
public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
        using(FormExport form = new FormExport())
            Application.Run(form);
	
		return null;
	}
	
	private enum ClipboardItemParserStateEnum
	{
        Text = 0,
        Image = 1,
        Files = 2,
	}
	
	private class ClipboardItem
	{
        public string Text {get;set;}
        public Bitmap Image {get;set;}
        public string[] Files {get;set;}
        
        public ClipboardItem()
        {
            this.Text = "";
            this.Image = null;
            this.Files = new string[0];
        }
        
        public ClipboardItem(string text)
        {
            this.Text = text;
            this.Image = null;
            this.Files = new string[0];
        }
        
        public ClipboardItem(string text, Bitmap image, string[] files)
        {
            this.Text = text;
            this.Image = image;
            this.Files = files;
        }
        
        public ListViewItem ToListViewItem()
        {
            ListViewItem item = new ListViewItem(this.Text);
            item.SubItems.Add( (this.Image == null) ? "No" : "Yes" );
            item.SubItems.Add( string.Join(", ", this.Files) );
            item.Tag = this;
            return item;
        }
	}
	
	private class FormExport : Form
	{
        private ComboBox cboLists;
		private ListView lvItems;
		private ColumnHeader colText;
		private ColumnHeader colImage;
		private ColumnHeader colFiles;
		private Button btnExport;
		
		public FormExport()
		{
            this.cboLists = new ComboBox();
			this.lvItems = new ListView();
			this.btnExport = new Button();
			this.colText = new ColumnHeader();
			this.colImage = new ColumnHeader();
			this.colFiles = new ColumnHeader();
		
            this.SuspendLayout();
            
            this.colText.Text = "Text";
            this.colText.Width =  140;
            this.colImage.Text = "Has Image";
            this.colFiles.Text = "Files";
            
            this.cboLists.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cboLists.Location = new Point(16, 16);
            this.cboLists.Size = new Size(296, 21);
            this.cboLists.Items.AddRange(new string[]{"History", "Local Pinned", "Recent Online", "Pinned Online"});                        
            this.cboLists.SelectedIndex = 0;
            this.cboLists.SelectedIndexChanged += this.cboLists_SelectedIndexChanged;
            
            this.lvItems.Columns.AddRange(new ColumnHeader[] { this.colText, this.colImage, this.colFiles });
            this.lvItems.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            this.lvItems.Location = new Point(16, 48);
            this.lvItems.Size = new Size(296, 336);
            this.lvItems.View = View.Details;            
            this.lvItems.FullRowSelect = true;
            
            this.btnExport.Click += this.btnExport_Click;
            this.btnExport.Text = "Export Selected Items to a File";
            this.btnExport.Location = new Point(16, 392);
            this.btnExport.Size = new Size(296, 32);
            
            this.ClientSize = new System.Drawing.Size(329, 439);
            this.Controls.Add(this.btnExport);
            this.Controls.Add(this.lvItems);
            this.Controls.Add(this.cboLists);
            this.Text = "ClipboardFusion Item Exporter";
           
            this.ResumeLayout(false);
            
            this.cboLists_SelectedIndexChanged(null, null);
		}
		
		private void btnExport_Click(object sender, EventArgs e)
		{
            string filename;
            using(SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.Filter = "ClipboardFusion Item List|*.cfitems";
                if(dialog.ShowDialog() != DialogResult.OK)
                    return;
                    
                filename = dialog.FileName;
            }
            
            using(FileStream file = File.OpenWrite(filename))
            using(StreamWriter writer = new StreamWriter(file))
            {
                foreach(ListViewItem lvItem in this.lvItems.SelectedItems)
                {
                    ClipboardItem item = lvItem.Tag as ClipboardItem;
                    if(item == null)
                        continue;
                        
                 
                    writer.WriteLine( Convert.ToBase64String(Encoding.Unicode.GetBytes( item.Text )));
                    if(item.Image == null)
                    {
                        writer.WriteLine();
                    }
                    else
                    {
                        using(MemoryStream stream = new MemoryStream())
                        {
                            item.Image.Save(stream, ImageFormat.Png);
                            writer.WriteLine( Convert.ToBase64String(stream.ToArray()));
                        }
                    }
                    
                    writer.WriteLine( Convert.ToBase64String(Encoding.Unicode.GetBytes( string.Join("|", item.Files) )));
                }
            }
            
            BFS.Dialog.ShowMessageInfo("The selected item have been exported from the " + this.cboLists.SelectedItem + " Items list.");
            this.Close();
		}
		
		private void cboLists_SelectedIndexChanged(object sender, EventArgs e)
		{
            this.lvItems.Items.Clear();
            string value = this.cboLists.SelectedItem as string;
            switch(value)
            {
                case "History":
                    for(int i = 0; i < BFS.ClipboardFusion.GetHistoryItemCount(); i++)
                    {
                        ClipboardItem item = new ClipboardItem(BFS.ClipboardFusion.GetHistoryText(i), BFS.ClipboardFusion.GetHistoryImage(i), BFS.ClipboardFusion.GetHistoryFileList(i));
                        this.lvItems.Items.Add(item.ToListViewItem());
                    }
                
                    break;
                case "Local Pinned":
                    for(int i = 0; i < BFS.ClipboardFusion.GetLocalPinnedItemCount(); i++)
                    {
                        ClipboardItem item = new ClipboardItem(BFS.ClipboardFusion.GetLocalPinnedText(i) , BFS.ClipboardFusion.GetLocalPinnedImage(i), BFS.ClipboardFusion.GetLocalPinnedFileList(i));
                        this.lvItems.Items.Add(item.ToListViewItem());
                    }
                
                    break;
                case "Recent Online":
                    foreach(string text in BFS.ClipboardFusion.CFOGetAllSavedText())
                        this.lvItems.Items.Add(new ClipboardItem(text).ToListViewItem());
                
                    break;
                case "Pinned Online":
                    foreach(string text in BFS.ClipboardFusion.CFOGetAllPinnedText() )
                        this.lvItems.Items.Add(new ClipboardItem(text).ToListViewItem());
                
                    break;
                    
            }
		}
	}
}