BCS - Business Computing Solutions

creating the future, today

  • Increase font size
  • Default font size
  • Decrease font size

Facebook Desktop Applications

Facebook have been developing their Connect platform for a while now, and although there are a few problems with it there are some really nice results you can achieve with the official Client libraries.

As the assingment was to write a program in visual basic, that is what I've done. However looking back on it now I do think that C# may have been better for some parts due to the ability to make the code object orientated.

 

Click read more to read the documentation written for the assignment, that goes with the application 

Programming Assignment - Task 2


The programming language which I was told to use was Visual Basic. It is a non object orientated language which means that the functions are not groups together in blocks like object orentated languages.
    Another feature of the language is the ability to create threads. This means that more than one process can happen, such as updating the screen, and doing a background task


The three programs are related an awful lot, however the focus of each one is definatly different.
    The first is designed to just allow a simple form which shows when friends come online. It is the smallest of the three and only has the simple features. The profile page is on a seperate form, as it is not normally needed. This means the main form can be smaller. so even if it is onscreen it is small enough to not block off too much.
    The second program was based around the searching through your friends as well as the features in the first section. All the data is displayed quickly and on the same form. It also starts building in other status features, allowing a two way interaction with facebook.
    The final program is the one which also starts to include Instant Messenger features, such as online/offline contact list and again minimising to tray. It is also designed so when the API allows LiveMessage API functions to be used in desktop applications, it can be intergrated quickly.

    The final feature of visual basic is the ability to easily intergrate other APIs into the programming, so they can intergrate seamlessly with the other code.

Threaded splash screenThis screenshot shows how the program starts to load. It is started on its own thread during the loading of Form1. It is set up so that if not progress has been specified for the progress bar a marquee style progress bar is displayed.
    This form also is borderless and has a background image which is embeded into resources.
    Another thing to note is the positioning, which is 'screen center' of primary monitor. This form does not need to be moved, which is useful because borderless forms that do need to move need to call methods from the user32.dll system file.
    Although it cannot be seen, white as a background colour is set to trasparent, which could mean the text could overlap the percieved box.

This is the only two public methods for the form, which are used to set status. As you will notice, they are not thread safe
Public Sub setWait(ByVal message As String, ByVal max As Integer)
'Set waiting message
Label1.Text = message
'Set maximum value
ProgressBar1.Maximum = max
'Set marquee style progress bar
ProgressBar1.Style = ProgressBarStyle.Marquee
End Sub

Public Sub setProgress(ByVal current As Integer)
'Progress the bar

'set the bar so it animates the movement of the progress, and more importantly
'overwrites the marquee style
ProgressBar1.Style = ProgressBarStyle.Continuous
'set the current progress
ProgressBar1.Value = current
End Sub

Main ScreenThis next screen shows the main window of the application It is split into four main function areas, the menu strip, status bar, friend list (searchable) and profile. This allows the user to see information about the users. As each application has a maximum request number, the data which was collected during startup about friends is cached into Collections (based on the IList interface class). This means that the only thing that needs updating on update is the online friend ID's, which are then referenced back to the cache to retrieve information fast.
    Although there are lots of loops to collect data within the program this is definatly faster and more effective than querying the facebook server everytime information is needed.

    The final thing you will notice is the notification balloon. This allows the user to see which friends have appeard online with every update the program does. These updates are set to 30 second intervals, before the information is queried, using Facebook's FQL (Facebook Query Language)

As the friends updates come in XML data the data needs to be passed and the online friends translated from the UIDs given in the XML into the user data, which is already in the cache (currentFriends). This code decodes the XML into an array of strings containing the UID's before looping through the cache to find the user data
Private Sub getOnlineFriends()
'Define output strings and XML documents, used for facebook responses
Dim output As String = ""
Dim result As String
Dim XMLResult As XmlDocument = New XmlDocument()
Dim UserNodes As XmlNodeList

'Send a query using the FQL
result = FacebookService1.API.fql.query("SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=" & FacebookService1.uid & ") AND 'active' IN online_presence")

'Load into XML document
XMLResult.LoadXml(result)

'get all UID nodes
UserNodes = XMLResult.GetElementsByTagName("uid")

'If some freinds are online, say!
If UserNodes.Count > 0 Then

Dim newOnlineFriends As New Collection(Of user)

'for each user add them to the new list of online friends
For Each userNode As XmlNode In UserNodes
For Each MyFriend As user In currentFriends
If (MyFriend.uid = userNode.InnerText()) Then
newOnlineFriends.Add(MyFriend)
End If
Next

Next


'cycle through new friends
For Each OnlineFriend As user In newOnlineFriends
'If they were not on last time then.....
If onlineFriends.Contains(OnlineFriend) = False Then
'Find user in prefeched data, saves on timly web calls
For Each MyFriend As user In currentFriends
If MyFriend Is OnlineFriend Then
'Add name to output with comma at the end
output = output & MyFriend.first_name & " " & MyFriend.last_name & ", "
End If
Next
End If
Next

If output.Length > 0 Then

'Trim the end space and comma
output = output.Substring(0, output.Length - 2)
'End output
output = output & " appeared online"

'Set balloon
NotifyIcon1.BalloonTipTitle = "New friends appeared online"
NotifyIcon1.BalloonTipText = output
'Show it for 5 seconds
NotifyIcon1.ShowBalloonTip(5000)

End If
'Replace old online friends with new list
onlineFriends = newOnlineFriends

End If


End Sub

The following code shows the update cycle which happens every 30 seconds
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Method called on timer interval reached


'gets new online friends list
getOnlineFriends()
'Did beep so I could work out when the timer had ticked
'Console.Beep()

Dim newTrack As IITTrack = iTunesIface.CurrentTrack

If currentTrack Is Nothing And autoStatusTrack Then
currentTrack = newTrack
setStatus(StatusString & " (Listening to " & newTrack.Name & " by " & newTrack.Artist & ")")
ElseIf currentTrack Is Nothing Then
currentTrack = newTrack
End If

If TypeOf newTrack Is IITTrack And TypeOf currentTrack Is IITTrack Then

If newTrack.trackID <> currentTrack.trackID And autoStatusTrack Then
setStatus(StatusString & " (Listening to " & newTrack.Name & " by " & newTrack.Artist & ")")
ElseIf StatusText.Text <> MyUser.status.message Then
setStatus(StatusText.Text)
End If

ElseIf StatusText.Text <> MyUser.status.message Then
setStatus(StatusText.Text)
End If

If StatusText.Focused = False Then
StatusText.Text = MyUser.status.message
End If
End Sub

Buddy List    As the popups only show which users have arrived online it is important to have an interface which can tell the user all the users that are online and offline. This form feeds off information that is within Form1 and simply displays them in a ListView to show online and offline contacts.

    When the facility for desktop applications is available to use the LiveMessage class, an instant messenger application could very easily be built from this.

    A test feature I have also included within the application is to be able to update your status with the current song you are listening to. There is a major problem with this at the moment, and that is as well as facebook applications having a limit to the number of times the status can be changed, the music also appears lots of times in your profile. An idea for facebook may be to intergrate a way of adding what song you are listening to.

Tray IconAs the application name suggests it must be able to be minimised to tray. This notification shows when it is docked, to alert the user to how to get the window back when they need it.

The following code shows how the buddy list is updated
Private Sub updateList()
Dim currentLVI As ListViewItem
Dim OfflineGroup As ListViewGroup = New ListViewGroup("Offline Contacts")
Dim OnlineGroup As ListViewGroup = New ListViewGroup("Online Contacts")
Dim buddyIcons As New ImageList()
Dim path As String = Application.StartupPath

FriendList.Groups.Clear()
FriendList.Items.Clear()

FriendList.LargeImageList = buddyIcons
FriendList.SmallImageList = buddyIcons

buddyIcons.Images.Add("online", New Icon("online.ico"))
buddyIcons.Images.Add("offline", New Icon("offline.ico"))

FriendList.Groups.Add(OnlineGroup)
FriendList.Groups.Add(OfflineGroup)

For Each onlineFriend As user In onlineFriends
currentLVI = New ListViewItem(onlineFriend.first_name & " " & onlineFriend.last_name)
currentLVI.Group = FriendList.Groups.Item(0)
currentLVI.ImageKey = "online"
FriendList.Items.Add(currentLVI)
Next

For Each MyFriend As user In allFriends
If onlineFriends.Contains(MyFriend) = False Then
currentLVI = New ListViewItem(MyFriend.first_name & " " & MyFriend.last_name)
currentLVI.Group = FriendList.Groups.Item(1)
currentLVI.ImageKey = "offline"
FriendList.Items.Add(currentLVI)
End If
Next
End Sub

Testing

The first test is a normal startup, looking for errors and inconsistancies in data displayed. As most of the data is cached on startup if there is anything wrong it is likely to pop up at this stage:

The first problem found was the startup screen is not calling the Paint() method after Show() has been called, because the thread is being blocked by other processes.
    The first attempt of solving this problem was rethreading the form with the Show() command taking another thread. As the Show() command however is not a blocking command and simply starts the form updating as soon as the method finished (in a comple of milliseconds) the form would disappear, redering the whole exercise useless
    The final method was to call the ShowDiaglogue() command on the other thread, which is a thread blocking command, and kept the form onscreen as well as updating on the other thread. At the end the thread was given the command Abort() which kills it.
    Although this is not exactly the most clean way of removing the form from screen, it does work. It would be interesting, should I have the tools to see if the resources had been released and the garbage collector collected the object at the end of the thread run. It it had not it might involve pinning memory locations down then manually chucking them to the garbage collector.

In the third program the startup screen had another upgrade, and was positioned center, and had borders removed and replaced with a background image.

All data however was collected successfully and was cast successfully to all types needed for the form.


The second test is the changing of data on the form and ensuring it calls the correct functions and updates the text boxes and profile items

The first problem found with this was the ITunes library was working fine.... while ITunes was running! When it was not Null exceptions were being thrown, which called for the whole section to do with music updating to be enclosed within a test ensuring that items were nto null.
 
    The next problem is the constant updating of the profile every 30 seconds with a new status update. Although this has not been mended satifactorally it has been modified to allow a checkbox to decide whether to update. At some point the logic might be changed to only update the "Listening to" part of the status when the main part is changed.


The final test is all buttons are doing as wanted without errors

One of the only problems with this was the "As you type" search, which allowed the list of friends to be shortened to the search results as you type. This is partly because of the number of items which are being serached. I have about 375 friends on facebook and searching through all those Linq objects will take some time! This was therefore replaced with a search button.
    Another update that could be included is the actions being done if "Enter" is pressed inside the textbox

Data types

There are a number of different data types which are supported in this program. Some of them are basic data types, such as string and Integers, used for storing user inputs and user data. These are standard to all programs written in the Visual Basic language.
    After this we have got some data types which need to be imported from certain places within the operating system libraries, such as lists (built a bit like arrays having Enumerable properties). Unlike most programs most of the lists in this program come out of the Microsoft.XML.Linq library, rather than the Microsoft.Collections library (although there are a few that come from here) The Linq library is designed to hold XML data in a structured format.
    The final main type of information which is got from the program is XML. This is a structured data language which all facebook responses come in. Although most of the responses are handled within the compiled facebook.dll API file, the online friends had to be done manaully seeing that there was no easy function to use.

Aknowledgements

The Facebook API library was a free library provided by microsoft. Although mostly just looking after calls to facebook, and encoding them into Linq objects, some of the windows form elements are also found within the library
Attachments:
FileDescriptionFile size
Download this file (Program 3.rar)Source CodeThis contains the source code of program 3. This contains all the features of program 1 & 2, plus some extra features757 Kb

Comments

B
i
u
Quote
Code
List
List item
URL
Name *
Code   
ChronoComments by Joomla Professional Solutions
Submit Comment