Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

Gett.NET - A library for Ge.tt Web Services

, 13 Jun 2014
Use Ge.tt Web Services for real-time file publishing and sharing.
Gett.NET_demo.zip
Debug
Gett.NET.dll
GettTest.exe
GettTest.vshost.exe
GettTest.vshost.exe.manifest
Newtonsoft.Json.dll
Gett.NET_src-noexe.zip
Gett.NET
Gett.NET.suo
Gett.NET.v11.suo
Gett.NET
bin
Json.Net
Properties
WebSocket
GettTest
bin
Debug
GettTest.vshost.exe.manifest
obj
x86
Debug
Properties
Settings.settings
Gett.NET_src.zip
Gett.NET.suo
Gett.NET.v11.suo
Newtonsoft.Json.dll
GettTest.vshost.exe
GettTest.vshost.exe.manifest
Settings.settings
/*

  Copyright (c) 2012 Togocoder
 
  This program is to illustrate how to use Gett.NET library that uses the Ge.tt API Web Service, http://ge.tt/developers
  Please see copyright text in the source code for Gett.NET.

*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GettTest
{
  public partial class GettSharingForm : Form
  {
    /// <summary>
    /// Illustrate of work...
    /// </summary>
    public void OtherWork()
    {
      // Login to Ge.tt
      Gett.Sharing.GettUser user = new Gett.Sharing.GettUser();
      user.Login("myapikey", "mymail@local", "mypassword");

      // Find specefic share
      Gett.Sharing.GettShare share1 = user.Shares.GetShare("9XwUVrB");

      // Create new file on share.
      Gett.Sharing.GettFile file1 = share1.CreateFile("mybackup.sql");
      
      // Start upload new file
      file1.UploadFile(@"d:\mysqlbackupdump\today.sql");

      // Get all shares with most files count in it ordered
      var shareWithMostfiles = from share in user.Shares.GetShares()
                               where share.FilesInfo.Length > 0
                               orderby share.FilesInfo.Length descending
                               select share;

      // Get all shares with the largeste files
      var shareWithTheLargestFileSize = from share in user.Shares.GetShares()
                                        where share.FilesInfo.Length > 0
                                        orderby share.FilesInfo.Max(f => f.Size) descending
                                        select share;

      // Begin async upload of file
      Gett.Sharing.GettFile file2 = share1.CreateFile("contacts.sql");
      file2.BeginUploadFile(@"d:\mysqlbackupdump\contacts.sql", new AsyncCallback(this.UploadFileCompleted), file2);

      // Begin async download of file
      file1.BeginDownloadFile(@"d:\download\today.sql", (ar) => file1.EndDownloadFile(ar), null);
    }

    /// <summary>
    /// Example on Begin/End method
    /// </summary>
    private void UploadFileCompleted(IAsyncResult ar)
    {
      Gett.Sharing.GettFile file2 = (Gett.Sharing.GettFile)ar.AsyncState;
      bool success = file2.EndUploadFile(ar);
    }

    public void OtherWork2()
    {
      // Login to Ge.tt
      Gett.Sharing.GettUser user = new Gett.Sharing.GettUser();
      user.Login("myapikey", "mymail@local", "mypassword");

      // Create new share
      Gett.Sharing.GettShare share1 = user.Shares.CreateShare(DateTime.Now.ToString("s"));
      Gett.Sharing.GettFile file1 = share1.CreateFile("MyDataFile." + DateTime.Now.ToString("s") + ".txt");

      // Upload own string as a file to Ge.tt
      string myDataString = "Hello from Gett.NET library";
      file1.UploadData(System.Text.Encoding.UTF8.GetBytes(myDataString));

      // Download file as a string or ...
      byte[] content = file1.DownloadData();
      string contentString = System.Text.Encoding.UTF8.GetString(content);

      if (myDataString == contentString)
        MessageBox.Show("It is a match!");

      // Download content to a local file
      file1.DownloadFile(@"C:\TEMP\MyFileFromGett.txt");

      // Delete file or ...
      file1.Destroy();

      // Delete share and all files
      share1.Destroy();
    }

    /// <summary>
    /// Main class, 1 instance pr. user login.
    /// </summary>
    Gett.Sharing.GettUser gettUser = new Gett.Sharing.GettUser(10);

    /// <summary>
    /// For use with Ge.tt Live API
    /// </summary>
    Gett.Sharing.Live.GettLive gettLive = null;

    public GettSharingForm()
    {
      InitializeComponent();
    }

    private void GettSharingForm_Shown(object sender, EventArgs e)
    {
      if (string.IsNullOrEmpty(Properties.Settings.Default.ApiKey) || string.IsNullOrEmpty(Properties.Settings.Default.Email) || string.IsNullOrEmpty(Properties.Settings.Default.Password))
      {
        this.button_Setup.PerformClick();
      }
    }

    private void GettSharingForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      if (this.gettLive != null)
      {
        this.gettLive.Stop();
      }
    }

    /// <summary>
    /// Settings, apikey, email and password
    /// </summary>
    private void button_Setup_Click(object sender, EventArgs e)
    {
      SettingsForm settings = new SettingsForm();

      if (settings.ShowDialog() == DialogResult.OK)
      {
        this.button_GettLogin.PerformClick();

      }
    }

    /// <summary>
    /// Login to Ge.tt.
    /// </summary>
    private void button_GettLogin_Click(object sender, EventArgs e)
    {
      try
      {
        this.gettUser.Login(Properties.Settings.Default.ApiKey, Properties.Settings.Default.Email, Properties.Settings.Default.Password);

        // Setup Live API ?
        if (!string.IsNullOrEmpty(Properties.Settings.Default.LiveSessionId))
        {
          try
          {
            // Startup Live API
            this.gettLive = this.gettUser.GetLive();
            this.gettLive.LiveFileEvent += (lfe) => this.Invoke(new Action(() => this.listBox_LiveEvents.Items.Add(string.Format("{0}: Type:{1} - Share:{2} - FileName:{3}", lfe.Timestamp, lfe.Type, lfe.ShareName, lfe.FileName))));
            this.gettLive.Start(Properties.Settings.Default.LiveSessionId, true);
          }
          catch (Exception ex)
          {
            MessageBox.Show("Failed to connect to Live API on Ge.tt" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
          }
        }
        else
        {
          this.label3.Visible = false;
          this.listBox_LiveEvents.Visible = false;
          this.listBox_Shares.Size = this.listBox_Files.Size;
        }

        this.button_RefreshShares.PerformClick();
      }
      catch (Exception ex)
      {
        this.label_GettMe.Text = "Not loggedin.";
        this.label_UserStorage.Text = "Storage...";
        this.listBox_Shares.Items.Clear();
        this.listBox_Files.Items.Clear();

        MessageBox.Show("Failed to login on Ge.tt, check apiKey, email or password" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
      }
    }

    #region Shares
    /// <summary>
    /// Create new share
    /// </summary>
    private void button_CreateShare_Click(object sender, EventArgs e)
    {
      NewShareForm newShare = new NewShareForm();

      if (newShare.ShowDialog() == DialogResult.OK)
      {
        try
        {
          Gett.Sharing.GettShare share = this.gettUser.Shares.CreateShare(newShare.Title);
          this.listBox_Shares.Items.Add(share);
        }
        catch (Exception ex)
        {
          MessageBox.Show("Failed to create new share at Ge.tt" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
        }
      }
    }

    /// <summary>
    /// Change title
    /// </summary>
    private void button_ChangeTitle_Click(object sender, EventArgs e)
    {
      if (this.listBox_Shares.SelectedItem is Gett.Sharing.GettShare)
      {
        Gett.Sharing.GettShare share = (Gett.Sharing.GettShare)this.listBox_Shares.SelectedItem;

        NewShareForm changeTitleShare = new NewShareForm();
        changeTitleShare.Share = share;

        if (changeTitleShare.ShowDialog() == DialogResult.OK)
        {
          try
          {
            share.SetTitle(changeTitleShare.Title);
            this.button_RefreshShares.PerformClick();
          }
          catch (Exception ex)
          {
            MessageBox.Show("Failed to change title for share at Ge.tt" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
          }
        }
      }
    }

    /// <summary>
    /// Delete Share
    /// </summary>
    private void button_DeleteShare_Click(object sender, EventArgs e)
    {
      if (this.listBox_Shares.SelectedItem is Gett.Sharing.GettShare)
      {
        Gett.Sharing.GettShare share = (Gett.Sharing.GettShare)this.listBox_Shares.SelectedItem;

        if (MessageBox.Show("Delete share '" + (string.IsNullOrEmpty(share.Info.Title) ? share.Info.ShareName : share.Info.Title) + "' ?", "Delete share", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
          try
          {
            share.Destroy();
            this.listBox_Shares.Items.Remove(share);
          }
          catch (Exception ex)
          {
            MessageBox.Show("Failed to delete '" + (string.IsNullOrEmpty(share.Info.Title) ? share.Info.ShareName : share.Info.Title) + "' from Ge.tt" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
          }
        }
      }
    }

    private void button_RefreshShares_Click(object sender, EventArgs e)
    {
      try
      {
        this.gettUser.RefreshMe();
        this.label_GettMe.Text = string.Format("Email:{0} ({1})", this.gettUser.Me.Email, this.gettUser.Me.FullName);
        this.label_UserStorage.Text = string.Format("You are using {0} of {1}, free {2}.", UploadDownloadProgress.FormatSizeDisplay(this.gettUser.Me.Storage.Used), UploadDownloadProgress.FormatSizeDisplay(this.gettUser.Me.Storage.Total), UploadDownloadProgress.FormatSizeDisplay(this.gettUser.Me.Storage.Free));

        this.listBox_Files.Items.Clear();
        this.listBox_Shares.Items.Clear();
        this.listBox_Shares.Items.AddRange(this.gettUser.Shares.GetShares());
      }
      catch (Exception ex)
      {
        MessageBox.Show("Failed to refresh shares from Ge.tt" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
      }
    }

    private void listBox_Shares_SelectedIndexChanged(object sender, EventArgs e)
    {
      this.listBox_Files.Items.Clear();
      if (this.listBox_Shares.SelectedItem is Gett.Sharing.GettShare)
      {
        Gett.Sharing.GettShare share = (Gett.Sharing.GettShare)this.listBox_Shares.SelectedItem;
        this.listBox_Files.Items.AddRange(share.Files);
      }
    }
    #endregion

    #region Files
    private void button_RefreshFiles_Click(object sender, EventArgs e)
    {
      this.listBox_Files.Items.Clear();
      if (this.listBox_Shares.SelectedItem is Gett.Sharing.GettShare)
      {
        Gett.Sharing.GettShare share = (Gett.Sharing.GettShare)this.listBox_Shares.SelectedItem;
        share.Refresh();
        this.listBox_Files.Items.AddRange(share.Files);
      }     
    }

    private void button_Download_Click(object sender, EventArgs e)
    {
      if (this.listBox_Files.SelectedItem is Gett.Sharing.GettFile)
      {
        Gett.Sharing.GettFile file = (Gett.Sharing.GettFile)this.listBox_Files.SelectedItem;

        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.OverwritePrompt = true;
        saveDialog.FileName = file.Info.FileName;

        if (saveDialog.ShowDialog() == DialogResult.OK)
        {
          UploadDownloadProgress download = new UploadDownloadProgress(false, saveDialog.FileName, file);
          download.Show();
        }
      }
    }

    private void button_Upload_Click(object sender, EventArgs e)
    {
      if (this.listBox_Shares.SelectedItem is Gett.Sharing.GettShare)
      {
        OpenFileDialog openDialog = new OpenFileDialog();
        openDialog.CheckFileExists = true;
        if (openDialog.ShowDialog() == DialogResult.OK)
        {
          Gett.Sharing.GettShare share = (Gett.Sharing.GettShare)this.listBox_Shares.SelectedItem;

          try
          {
            Gett.Sharing.GettFile file = share.CreateFile(System.IO.Path.GetFileName(openDialog.FileName), Properties.Settings.Default.LiveSessionId);
            UploadDownloadProgress upload = new UploadDownloadProgress(true, openDialog.FileName, file);
            upload.Show();
          }
          catch (Exception ex)
          {
            MessageBox.Show("Failed to upload new file '" + openDialog.FileName + "' to Ge.tt" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
          }
        }
      }
    }

    private void button_Delete_Click(object sender, EventArgs e)
    {
      if (this.listBox_Files.SelectedItem is Gett.Sharing.GettFile)
      {
        Gett.Sharing.GettFile file = (Gett.Sharing.GettFile)this.listBox_Files.SelectedItem;

        if (MessageBox.Show("Delete file '" + file.Info.FileName + "' ?", "Delete file", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
        {
          try
          {
            file.Destroy();
            this.listBox_Files.Items.Remove(file);
          }
          catch (Exception ex)
          {
            MessageBox.Show("Failed to delete '" + file.Info.FileName + "' from Ge.tt" + Environment.NewLine + "Exception of type " + ex.GetType().ToString() + Environment.NewLine + ex.Message);
          }
        }
      }
    }
    #endregion
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kim Togo
Software Developer Miralix
Denmark Denmark
Has worked as a programmer since 1999, starting with C++/MFC, Java, PHP and MySQL. Now it is all about C# (my favorite programming language), MS SQL, Azure and Xamarin (iOS/Android/WP8)
 
My primary work is to create applications that interacts with PABC/PBX, Microsoft Lync / UCMA.
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 13 Jun 2014
Article Copyright 2012 by Kim Togo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid