Friday, December 26, 2008

As3 flip Horizontal / flip Vertical a displayObject

public function flipHorizontal(dsp:DisplayObject):void
{
var matrix:Matrix = dsp.transform.matrix;
matrix.a=-1;
matrix.tx=dsp.width+dsp.x;
dsp.transform.matrix=matrix;
}

public function flipVertical(dsp:DisplayObject):void
{
var matrix:Matrix = dsp.transform.matrix;
matrix.d=-1;
matrix.ty=dsp.height+dsp.y;
dsp.transform.matrix=matrix;
}

Resource: http://www.dreaminginflash.com/as3-flip-horizontal-flip-vertical-a-displayobject/

Thursday, December 11, 2008

IMDB Movie Catalog with Adobe AIR

Hey guys,

I recently needed a program to lookup the IMDB ratings of the movies in my Movies folder. My movies are each in its own folder. It goes like this:

Casablanca
Click
Cloverfield
Conspiracy Theory
Die Hard 1
Die Hard 3
Eastern Promises
.
.
.

and so on. So regarding the number of movies it is a very difficult thing to find a movie to watch which has a good IMDB rating. So I decided to write a program which iterates through all the folder names in my Movies folder and looks them up in IMDB.

[UPDATE] - I recently updated the program to be able to read the filenames like: "Saving.Private.Ryan.720p.HDTV.Premiere.DTS.x264-ESiR.mkv". Even if the movie folders and files are in the same directory it should work fine.

the code can be seen here.

Also I am attaching a windows setup file for non-developers out there. You can contact me for its bugs. I will try to update it time to time.

Windows Setup File



Adobe Air File Format



have fun.

Note: Double click on items opens the IMDB page of that movie!


Friday, November 14, 2008

Read Flash Shared Objects with C++

Hi everyone,

Recently we needed to read the flash shared objects with an external application.

The problem was this: We have a setup file. This file can be downloaded from a bunch of different download locations. We need to do some stuff in the installer according to the download location. Like this: We may download dynamic content within the installer if the setup.exe is downloaded from location A.

Anyways. My first try was to put cookies while downloading the setup.exe. However we can't know which browser the user uses. And also there is another potential danger which is that browsers may change their way of storing their cookies. So we decided to go with flash shared objects as long as almost everybody has flash installed.

So before initiating the download we put a flash shared object to users machine. It is a GUID string in between two '_'. Something like this: _AF2504E0-4F89-11D3-9A0C-0305E82C330A_

Flash is storing shared objects on binary files. (For XP: C:\Documents and Settings\[USERNAME]\Application Data\Macromedia\Flash Player\#SharedObjects\[RANDOMFOLDERNAME])

So my first plan of reading the shared objects with a batch file failed. So I decided to write a small c++ app which could read the shared object I have written just before the download. After a day of learning C++ my the source code looks like below:




#include "stdafx.h"
#include <fstream>
#include <windows.h>
#include <iostream>
#include <stdio.h>

using namespace std;

// Flash stores the files in a random folder under #SharedObjects folder.
// So I had to find the name of that folder and return it.
wstring FindRandomFolderName(LPCWSTR SharedObjectPath)
{
// first we need to find the shared objects folder.
HANDLE hFind;
WIN32_FIND_DATA DataStructure;

hFind = FindFirstFile(SharedObjectPath, &DataStructure);
wstring folderName = L"";

if(hFind == INVALID_HANDLE_VALUE)
{
// As long as the path in the first parameter is correct we shouldn't hit this line.
}
else
{
// Find the last folder in the list. The first is "." second is ".."
while(FindNextFile(hFind, &DataStructure)!=0)
{
folderName = DataStructure.cFileName;
}

if(hFind != INVALID_HANDLE_VALUE)
{
//cout<<DataStructure.cFileName<<endl;
}
else
{
printf ("Invalid file handle. Error is %u\n", GetLastError());
}

FindClose(hFind);
}
return folderName;
}

int _tmain(int argc, _TCHAR* argv[])
{
if(argc==1)
{
cout << "Invalid number of arguments!\n";
return 0;
}

wstring path = argv[1];
LPCWSTR star = L"*";
wstring pathWithStar = path;
LPCWSTR domainNameAndFileName = L"\\www.pagefish.com\\referrer.sol";

pathWithStar.append(star);

wstring folderName = FindRandomFolderName((LPCWSTR)pathWithStar.c_str());

path.append(folderName);
path.append(domainNameAndFileName);

long begin, end, byteCount;

ifstream myFile;

myFile.open((wchar_t*)path.c_str(), ios::in | ios::binary);
//myFile.open(argv[1], ios::in | ios::binary);

begin = myFile.tellg();
myFile.seekg (0, ios::end);
end = myFile.tellg();
myFile.seekg(0, 0);
byteCount = end - begin;

char *buffer;
char referrerId[36]; // this is a guid saved in the shared object file
buffer = new char [byteCount];

myFile.read (buffer, byteCount);

if (!myFile)
{
// An error occurred!
}

myFile.close();

bool readReferrer = false;
int targetIndex = 0;

ofstream outFile;
outFile.open("referrerid.txt");

for (int i=0; i<byteCount; i++)
{
if(buffer[i]=='_' && readReferrer==false)
{
readReferrer=true;
}
else if(buffer[i]=='_' && readReferrer==true)
{
readReferrer=false;
}
if(readReferrer && buffer[i]!='_')
{
referrerId[targetIndex++] = buffer[i];
outFile << buffer[i];
}
}

outFile.close();

return 0;

}

Friday, November 7, 2008

File Download Manager in Actionscript

Hey Guys,

I have developed a file download manager for my client. This is a actionscript 3.0 library written in Flex Builder 3.0.

You can find the Google Code Project for filedownloadmanager here.

Friday, September 19, 2008

Realtime Applications with Flex & Actionscript

Hi everybody. We are developing an SDK for developers out there to easily program realtime applications. (Chat, Multiplayer Games etc)

You can find more info here.

Minimizing an AIR Application to system tray

Adobe AIR applications are windows applications. So they have all the possibilities like any other windows application like using the system tray.

First of all you have to prevent the application to close when clicked on the X close button on the window. To do that you have to trap the "closing" event of the WindowedApplication class.


<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="ApplicationComplete()" closing="MainWindow_OnClosing(event)">

or with actionscript:


this.addEventListener(Event.CLOSING, MainWindow_OnClosing);

When the user clicks on the close button on the corner of the main window MainWindow_OnClosing() function gets called. In this function you have to prevent the default action for the event which is closing the window. The reason why we listen to the "closing" event instead of "close" event is that the window is not closed yet. If you listen to the "close" event you can't prevent the window from getting closed.

So our closing event handler looks like this:


protected function MainWindow_OnClosing(event:Event):void
{
event.preventDefault();
this.visible = false;
}

We didn't close the window but we made it invisible. But how is the user going to interact with our program now? Yes. With the tray icon. So here is the sample code to setup a tray icon:


// A Loader to load the image for the icon png
var icon:Loader = new Loader();

// Not all the operating systems have to support tray icons.
// We just check for it. Windows supports it as you probably know. :)
if (NativeApplication.supportsSystemTrayIcon)
{
// When the user clicks the icon on the tray "TrayIcon_Click" event handler will be called.
NativeApplication.nativeApplication.icon.addEventListener(MouseEvent.CLICK, TrayIcon_Click);

// When the icon gets loaded it will call the iconLoadComplete event handler.
icon.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadComplete);

// We actually start loading an icon from the hard drive.
// This could be an icon on the internet as well
icon.load(new URLRequest("images/icons/icon16x16.png"));

// Lets get a handle of the trayIcon to change its tooltip.
var systray:SystemTrayIcon = NativeApplication.nativeApplication.icon as SystemTrayIcon;
systray.tooltip = "Your Application Tooltip";
}

Below you can see the handler for the icon load event.


private function iconLoadComplete(event:Event):void
{
NativeApplication.nativeApplication.icon.bitmaps = [event.target.content.bitmapData];
}

For best performance the icon image should be a 16x16 PNG. (At least in windows this is so).
So what happens when the user clicks the icon in the tray?


protected function TrayIcon_Click(event:Event):void
{
// Do anything here.
}

It is up to your imagination. :) You could do a bunch of things here. You can make the main application visible.

I can hear you guys asking how to add a menu to your tray icon. Ok! Adding a menu to your tray icon is very simple. Below there is the modifed version of the example code at the top:



// A Loader to load the image for the icon png
var icon:Loader = new Loader();

// Not all the operating systems have to support tray icons.
// We just check for it. Windows supports it as you probably know. :)
if (NativeApplication.supportsSystemTrayIcon)
{
// When the user clicks the icon on the tray "TrayIcon_Click" event handler will be called.
NativeApplication.nativeApplication.icon.addEventListener(MouseEvent.CLICK, TrayIcon_Click);

// When the icon gets loaded it will call the iconLoadComplete event handler.
icon.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadComplete);

// We actually start loading an icon from the hard drive.
// This could be and icon on the internet as well
icon.load(new URLRequest("images/icons/icon16x16.png"));

// Lets get a handle of the trayIcon to change its tooltip.
var systray:SystemTrayIcon = NativeApplication.nativeApplication.icon as SystemTrayIcon;
systray.tooltip = "Your Application Tooltip";

// Lets add a menu to our tray icon
var iconMenu:NativeMenu = new NativeMenu();
var menuCommand:NativeMenuItem = iconMenu.addItem(new NativeMenuItem("Exit"));
menuCommand.addEventListener(Event.SELECT, Exit_Handler);

// Assign our menu to the icon. Thats it.
systray.menu = iconMenu;
}

Here we have added a menu item which will close the application when clicked on. To close the application we call the exit() function of the native application. Here is the Exit_Handler function:


protected function Exit_Handler(event:Event):void
{
// Actually exiting the application it self removes the items from the tray.
// But just to give an example I clear the icons.
NativeApplication.nativeApplication.icon.bitmaps = [];

NativeApplication.nativeApplication.exit();
}

Here are the example source codes.