×
Namespaces

Variants
Actions
(Difference between revisions)

Dropbox with Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
jupaavola (Talk | contribs)
(Jupaavola -)
jupaavola (Talk | contribs)
(Jupaavola -)
Line 37: Line 37:
 
Download SharpBox release 1.1 SR2 or older. Newer release 1.2 does not work so well with Windows Phone and have few bugs when logging and authenticating to Dropbox service.
 
Download SharpBox release 1.1 SR2 or older. Newer release 1.2 does not work so well with Windows Phone and have few bugs when logging and authenticating to Dropbox service.
  
Extract downloaded zip file and add two DLL, '''AppLimit.CloudComputing.SharpBox.dll'' and '''Newtonsoft.Json.Silverlight.dll'''', files from '''sl3-wp''' folder as reference to your project.
+
Extract downloaded zip file and add two DLL, '''AppLimit.CloudComputing.SharpBox.dll''' and '''Newtonsoft.Json.Silverlight.dll''', files from '''sl3-wp''' folder as reference to your project.
  
 
== Sharpbox prerequisities ==
 
== Sharpbox prerequisities ==
  
Sharpbox needs valid Dropbox application key and secret. These you can make in Dropbox website, developers section where you create own application.
+
Sharpbox needs valid Dropbox application key and secret. These can be made in Dropbox website, developers section where you create own application. For testing and developing, you don't need to apply for production keys.
  
 
Dropbox developers: https://www.dropbox.com/developers
 
Dropbox developers: https://www.dropbox.com/developers
Line 53: Line 53:
 
Note that used functions from Sharpbox are asynchronous and those are not run in UI thread where your application is running. Still it is possible to call your application functions or change member variables from callback functions by using dispatcher.
 
Note that used functions from Sharpbox are asynchronous and those are not run in UI thread where your application is running. Still it is possible to call your application functions or change member variables from callback functions by using dispatcher.
  
Example function '''CallbackFunction''' is asynchronous callback function. This shows how to use dispatcher to call '''parseFilesAndDirectories''' function what is normail private function in application thread.
+
Example function '''CallbackFunction''' is asynchronous callback function. This snippet shows how to use dispatcher to call '''parseFilesAndDirectories''' function what is normail private function in application thread.
  
 
<code cpp>
 
<code cpp>

Revision as of 13:20, 6 September 2012

This article explains how to connect Windows Phone to Dropbox cloud service by using Sharpbox

WP Metro Icon File.png
WP Metro Icon Web.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
CompatibilityArticle
Created: jupaavola (31 Aug 2012)
Last edited: jupaavola (06 Sep 2012)

Contents

Introduction

Some user may want to connect other cloud services than SkyDrive which is offered by Microsoft. By using Open Source project called Sharpbox, you can access to Dropbox clouds. At the time of writing this article, it is still not possible to access other clouds like CloudMe in Windows Phone although those others works with desktops.

Summary

Required software

Go to Sharpbox website: http://sharpbox.codeplex.com/

Download SharpBox release 1.1 SR2 or older. Newer release 1.2 does not work so well with Windows Phone and have few bugs when logging and authenticating to Dropbox service.

Extract downloaded zip file and add two DLL, AppLimit.CloudComputing.SharpBox.dll and Newtonsoft.Json.Silverlight.dll, files from sl3-wp folder as reference to your project.

Sharpbox prerequisities

Sharpbox needs valid Dropbox application key and secret. These can be made in Dropbox website, developers section where you create own application. For testing and developing, you don't need to apply for production keys.

Dropbox developers: https://www.dropbox.com/developers

Using Sharpbox

Sharpbox library has synchronized and asynchronized functions for many operations. For desktop usage both are fine but in Windows Phone, operating system and it's frameworks has their own limitations. All of the synchronized calls will block running UI thread and therefore the whole application. The only way to get things working is to use asynchronous functions. Some needed functions still lack of asynchronous versions but it is possible to use these by implementing own async versions to own application.

Sharpbox does not run in UI thread

Note that used functions from Sharpbox are asynchronous and those are not run in UI thread where your application is running. Still it is possible to call your application functions or change member variables from callback functions by using dispatcher.

Example function CallbackFunction is asynchronous callback function. This snippet shows how to use dispatcher to call parseFilesAndDirectories function what is normail private function in application thread.

void CallbackFunction(IAsyncResult result)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (fs != null)
{
parseFilesAndDirectories(fs);
}
});
}

Logging to Dropbox

This is pretty easy, create credentials and configuration object. Then call BeginOpenRequest and wait for callback.

        public void ConnectCloud(string username, string password)
{
DropBoxCredentials creds = new DropBoxCredentials();
creds.ConsumerSecret = APP_SECRET;
creds.ConsumerKey = APP_KEY;
creds.UserName = username;
creds.Password = password;
 
m_dropBox.BeginOpenRequest(LoginCallback, mCloudConfig, creds);
}

And the callback function for open request. Token of Dropbox connection is extracted from asynchronous functions result. This token also determines did connection succeed; if value is null, connection failed and in other values it succeeded. Without having token value, cannot continue because Sharpbox library does not know is service there.

        void LoginCallback(IAsyncResult result)
{
ICloudStorageAccessToken token = m_dropBox.EndOpenRequest(result);
if (token != null)
{
m_dropBox.BeginGetRootRequest(RootCallback);
}
else if (m_dropBox.IsOpened)
{
m_dropBox.BeginGetRootRequest(RootCallback);
}
}
<code>
 
== Get Dropbox file listing ==
 
After successful connection, we can proceed to reading files from Dropbox. As seen in '''LoginCallback''', '''RootCallback''' function is callback for '''BeginGetRootRequest''' asynchronous function.Before reading any file from Dropbox, you need to know root where start. After having root, it's possible to read files from anywhere. So reading root folder is important only in the beginning. And from reading root callback, again jump to another async callback function.
 
<code cpp>
private void RootCallback(IAsyncResult result)
{
ICloudDirectoryEntry root = m_dropBox.EndGetRootRequest(result);
if (root != null)
{
m_dropBox.BeginGetChildsRequest(ChildCallback, root);
}
}

After having child objects of root, any file or dir in topmost level, start parsing results.

        private void ChildCallback(IAsyncResult result)
{
 
List<ICloudFileSystemEntry> fs = m_dropBox.EndGetChildsRequest(result);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (fs != null)
{
parseFilesAndDirectories(fs);
while (m_directories.Count > 0)
{
ICloudDirectoryEntry e = m_directories[0];
m_directories.RemoveAt(0);
m_dropBox.BeginGetChildsRequest(ChildCallback, e);
}
}
});
}

And how to loop results list and populate file list.

        private void parseFilesAndDirectories(List<ICloudFileSystemEntry> direntry)
{
foreach (ICloudFileSystemEntry entry in direntry)
{
System.Diagnostics.Debug.WriteLine("entry: " + entry.Name);
if (entry is ICloudDirectoryEntry)
{
m_directories.Add((ICloudDirectoryEntry)entry);
}
 
if (entry is ICloudFileSystemEntry && entry.Name.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase))
{
Files.Add(new CloudItem() { Name = entry.Name, Entry = entry });
}
}
}
627 page views in the last 30 days.
×