2013/04/14

GWT

If you find yourself for some aweful reason working with GWT, do yourself a favor.
Add the following line to 'Advanced settings' of 'GWT Compile' dialog:
-draftCompile -localWorkers X
X being the number of cores in your CPU.
You can thank me later :)

(+Boris Daich, you might find it helpful)

2012/05/28

Pesky Etching

While working with Qt buttons I've came across a very nasty behaviour - when you disable a button, it's label receives a pretty ugly shadow that makes the font rather unreadable. This effect is called 'etching'. There is a reminant of a CSS property that controls that behaviour called etch-disabled-text, but it's undocumented and actually doesn't work at all.

There is NO documented way of removing that shadow. Pretty crappy, especially for a very configurable toolkit like Qt.

Anyways, here's a workaround. It'll set the proper global palette rules that will apply this fix to all the buttons.
The first line sets the color of the disabled text, the second - removes the etching.


2012/01/22

OATMEAL2PDF

As a continuation to my wildly popular XKCD2PDF post, here's a script I whipped out to get me some sweet PDFs of a great comic called The Oatmeal.


2012/01/03

SCons and Qt Resource files

Today I've stumbled upon a bug/problem with SCons' support for Qt resource files (the ones with QRC extension).
Usually, to add a QRC to your project you add a line like this:
qrcobj = programEnv.Qrc("SomeFile.qrc", QT4_QRCFLAGS="-name SomeFile")
and all is well.
But - probably due to a bug somewhere in qt4.py module, the files that are referenced from inside the QRC (the actual images and stylesheets) are not added as a dependency to the build process, so when you change a CSS file, the QRC will not be rebuilt.
The snippet below parses the QRC manually, fetches the list of files inside and adds the to deps list; Python rocks.


2011/12/06

warning MSB8015

If you try to build a project in Visual Studio 2010 and you get the following error:

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(151,5): warning MSB8015: Forcing a rebuild of all source files due to the contents of ".........\Debug\custombuild.command.1.tlog" being invalid.

Don't despair. I tried googling it and came up with a total of 3 (THREE) results in the whole web, two being in Korean. Yikes.
In any case, the solution was rather simple - check line ending inside the project file. If for some reason (due to being committed to SCM incorrectly, or something of similar nature) it has improper line endings, VS2010 will think that you have bad symbols inside build commands and make the build result invalid.
In my case, I had CR-CR-LF line endings - which, after being changed to CR-LF - solved the issue.

2011/10/31

XKCD2PDF

This one doesn't neccessary fall under the topic of this blog, but since it's a hacky thing I've done - I'm posting it here.

Some time ago I wanted to get all the XKCD comics stored on my iPad for offline viewing. Didn't find another solution so I cooked my own:


  1. A script that gets the JSON files of each comics and rips the important info
  2. Download the stuff
  3. Build a HTML page describing the comics with their 'alt' part (the good stuff)
  4. Open the HTML in Word, set margins to zero, save as PDF
  5. ...
  6. Profit!


I am really sorry about the 4th step, I know it's lame, but suprisingly, Word's "Save-as PDF" feature gave the best looking output.

And if you are lazy, you can grab the already generated PDFs here: (split in 3 chunks, the first 900 comics).

http://www.filesonic.com/file/2840295035/out.pdf
http://www.filesonic.com/file/2840295055/out2.pdf
http://www.filesonic.com/file/2840295065/out3.pdf


P.S. Here's the script:





2011/05/30

Checking if MPMediaItem exists by URL

If you are usings AVPlayer with Asset URLs, you might want to check asset's existence on the device.
If the asset is a file in local folder - no problem, you can use NSFileManager to check for existance? But what if it's inside the iPod library? The following trick wasn't easy to find, but here it is:

   NSURL* furl = [f trackUrl];
//        ipod-library://item/item.mp3?id=
        if([[furl scheme] isEqualToString:@"ipod-library"]){
            NSNumber* pid = [NSNumber numberWithLongLong: [[f.name substringFromIndex:32] longLongValue] ];;
            MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:pid forProperty:MPMediaItemPropertyPersistentID];
            MPMediaQuery *songQuery = [[[MPMediaQuery alloc] init] autorelease];
            [songQuery addFilterPredicate: predicate];
            if (songQuery.items.count == 0) {
                return NO; // NOT FOUND!
            }

2011/05/26

Tracing Objective-C Allocation

So, let's say you have a problem tracking down some pesky retain/autorelease issue in your code. You can use NSZombieEnabled to catch double-releases, but sometimes those happen inside somebody else's code, and it's hard to track it down.

Based on a code I've found in this blog post, I've wrote a template for subclassing somebody else's class to print out the allocation history.

Here's how it looks:



And here's how you use it (in this example, I want to track AVPlayer):


SYNTESIZE_TRACE(AVPlayer)

AVPlayer* p = [[TraceAVPlayer alloc] init];


All done. Now, while running, you'll get a record of all retainCount modifications, with their stack trace.

2011/03/10

Making QT behave properly on Mac.

Couple of things I found out that helped porting a QT application to Mac:

The QMainWindow you create does not look like a native Cocoa window. The status bar is there, no matter what you request, the menu doesn't port and the icon is yucky.
Here's what you can do:

Remove status bar/resize grip:


Don't show unneeded icon in the title bar and move a QAction to Mac's main menu:

2011/01/26

SIMBL For Poking Inside Mac Application Internals

SIMBL is, according to it's Home Page:

Problem:
Some applications do about 90% of what I want.
Solution:
Develop my own applications.
Better Solution:
Patch the application myself...
SIMBL (SIMple Bundle Loader) - pronounced like "symbol" or "cymbal" - enables hacks and plugins. For instance, SIMBL enables PithHelmet to enhance Safari.

So, let's say we have a naughty program that has a behaviour we don't like. How would we treat that problem?


  1. class-dump TargetProgram
  2. Look at the list of classes and note the one that seems to be the issue. Let's call that class Victim.
  3. Inside, find your naughty method, let's call it '- (BOOL) victimMethod;'
  4. Open Info.plist of the victim, and note the bundle name and version
  5. Create new Cocoa bundle in XCode according to the steps on SIMBL site
  6. Fill in the bundle name as stated
  7. Use the following code: 
  8. Restart the target app
  9. ???
  10. Profit!

2010/11/25

Gallery, clicking and selecting

Android SDK includes a nice-looking component named Gallery.
It's supposed to provide you with functionality of flickable view-changer.
It get's an BaseAdapter and displays views it receives.
All good until you actually start using it.

First issue: 
setSelection(index, animated) method ignores the second parameter. There's no 'todo' anywhere, 'deprecated' or even acknowledgement from Google. It just doesn't give a crap what you pass there. It always navigates to your item without animation.
Now let's say that I want to change items in the gallery every X seconds. How can I make a pretty animation showing this lovely change? By hacking the bastard. Basically, the animations are initiates by the Fling motion that the component detects. If you simulate the correct fling - you will get the View flip animation.

gallery.onRealFling(null, null, -800, 0);
Here's a tricky part - the X velocity parameter needs to be adjusted per application, because to fling a larger view, you need a bigger velocity. So play with that value until you get a smooth transition.


Second issue:
Items inside the layout that you return in your adapter cannot receive click events. They just can't. No good reason for it, but someone botched up the bubbling up of mouse events in the Gallery, so now you can't have clickable items. If you set any one of them as clickable - the gallery stops handling dragging.
But fear not - you can fix it!
The solution is as follows - you add a touch listener to the gallery, and catch a Single Tap event (I did it with gesture listener), to differentiate it from the dragging that the gallery needs to handle.
Then, from the the location of the touch event, you can calculate where exactly inside your sub view the click was made - and from that you can handle the actual click.

Here's the code:

2010/10/24

Making a real marquee out of Android TextView

Android TextView supports a Marquee ellipsis method, which starts animating the text in the TextView if it's width exceeded the width of the view.
But let's say you want it to animate the text constantly? Here's a classic trick - pad the string until it goes out of the width of the view, so it will start the scrolling.


The hard part here was to find out how to calculate the line width (since the TextView itself doesn't supply that info. Had to dig android sources for that.

2010/09/22

A PDF Intent

Here's a naughty bit of behavior from the Android's Intents.
When you want to customize the mime-type of the requested intent, the setter for reason resets the previously set Data parameter. Cute right?
And to overcome this great issue, the API has another method setDataAndType(). See what they did there?
Anyways - here's a correct method (opening a PDF in this example):



NullPointerException Confusion

It's good to know that our field is called Computer Science. We're scientists. We like facts, laws and rules... Yeah, right. You think that if you've learned the basics of Data Structures, Algorithms and other courses in your B.Sc. it would prepare you for the real world. Nope. It's craaaaazy out there.

Today I had a real head-scratching moment when a completely innocent 'hashtable.put(key,val)' started giving me NullPointerException. Now, since I do know how hashes work, all I checked was - is the table itself null ? Nope, it's good. So what the hell?

Here comes the scary part:

put

public V put(K key,
             V value)
Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.The value can be retrieved by calling the get method with a key that is equal to the original key.
Specified by:
put in interface Map<K,V>
Specified by:
put in class Dictionary<K,V>

Parameters:
key - the hashtable key.
value - the value.
Returns:
the previous value of the specified key in this hashtable, or null if it did not have one.
Throws:
NullPointerException - if the key or value is null.
Apparently the usual approach is not good for the Java folk. They don't want no stinking nulls in their precious hashes!
The reasoning behind the idiocity is this - the default behavior of get() method in hashtable is to return null if key was not found. So when the designers of the API chose that wrong path, they had to follow it up with a wronger path, since they couldn't distinguish between a null from 'no key found' and 'null found  as a value'.

I hate people.

2010/09/15

GridViews, Buttons, Oh My.

In Android, there is a versatile class called GridView which can display any custom designed cells. You provide it with a layout for each item, fill in the values - and voila - it's working.
All is well, until you try to add something interactive to the Grid cell - like a button - you come across an interesting effect.
If you add a onClick listener to both the GridView and the Button inside the GridView - only one will get the click event. And you won't guess which one :) (hint: it's the button)
So basically, you can have interactivity - but not paired with OnItemClick events of the Grid.
Bummer, right?
Wrong. This can be rather easily fixed, and the problem lays in the way Android handles focus resolution between parent and child Views. For ListView you can play with Focusability parameter which controls who reacts first, and on GridView, just the the Button's Focusable parameter to false - and it just works. It still gets the touch events which cause the click, but it doesn't interfere with the focus mechanics of the Grid.
You're welcome!

2010/07/18

The Saga of COMException (0x800736b1)

So I had a very lovely weekend being sexually assaulted by a pesky problem while installing an ASP.NET application on a Windows XP IIS server.


Server Error in '/AppName' Application.


This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)



Exception Details:System.Runtime.InteropServices.COMException: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0x800736B1)


Ok, this is lovely. Tracing the error code was pretty straight forward and pointed to the lack of VC++ Redistributable DLLs on the target machine. They are present on the developer box, but are missing on target. Sounds easy enough, downloaded vc_redistx86.exe , installed - same result.

Excellent. So, using depends.exe, I checked that the DLL in question is looking for msvcr80.dll and friends. Curiously enough - they were installed on the system, but the problem persisted, the application just didn't pick them up.

So I dug some more, and came across a folder \Windows\WinSxS (http://msdn.microsoft.com/en-us/library/aa376307(VS.85).aspx). It's Microsoft's solution to DLL Hell. There are several versions of the same CRT DLLs, in subfolders with different version names.

Which pointed me to the fact that it's not that the DLL is missing, but it's of incorrect version. So what was left is to discover how exactly does the application select which DLL to load. (it's obviously not by name!). So after frantic Google searches I came across a beast whom I met before but didn't pay attention to - "XP Manifest". 

It's a XML file which is embedded inside the application/DLL as a resource, and can define properties like 'Dll dependency'. So a quick run of ResHacker, patching the XP Manifest, saving the new app, re running, seeing that it works and then promising my self to tear away the nads of whomever though it would be a great idea to make me spend my weekend tracking this shit, and not just showing me a freaking help message!

2010/07/05

Bitmap Button in Blackberry

I won't bitch about the state of Blackberry UI framework, just mention that there's isn't a standard button class that supports images. WTF?
In any case, here's the code that implements one.

Open an external browser on Android

Ok, so I wanted to open an external browser from my Android application. Crazy, right? Anyways, after some searching and trying to read almost empty Android SDK docs on the subject, I came across this golden turd:


        try{  
            Intent i = new Intent();
            ComponentName comp = new ComponentName(
                             "com.google.android.browser",
                                    "com.google.android.browser.BrowserActivity");
            i.setComponent(comp);
            i.setAction("android.intent.action.VIEW");
            i.addCategory("android.intent.category.BROWSABLE");
            Uri.Builder z = new Uri.Builder();          
            i.setData(z.build());
            getContext().startActivity(i);
        } catch (URISyntaxException e) {
                e.printStackTrace();
        }



At this point I lost all faith in humanity and all that's sacred.
Come on people, there must be a better way!

And here I come to save the day :) The code is actually very short and simple:


getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

2010/06/26

Inter-process Communication of BlackBerry

Maybe I just don’t know how to read the docs, or how to Google, but I was looking for this info way too much, somehow it was burried. In any case – let’s say you have to apps on the device, and they use a common PersistenStore. One app adds stuff there in the background. The other – shows a UI with a list of the items from the store. All nice and synchronized. The only problem is – there is no way to detect that the PersistentObject that you are using was updated! So if you want to have your list refreshed automatically – you have to get creative.

On the producer side, after updating the PersistentStore, post a global notification with a unique ID:
ApplicationManager.getApplicationManager().postGlobalEvent(Main.NOTIFICATIONS_ID_1);

On the consumer side (UI), register for the notifications, then catch that message and update the UI accordingly:

Why I Hate BlackBerry. Also, how to make a folder.

Let’s be clear, RIM should be ashamed of the way their developer toolkit looks. The documentation is sparse at best, their own ide JDE looks and works like something from the 70s, and their Eclipse plugin refuses to work with actual devices and crashes on Simulator hot swaps. I am not even mentioning the fact that the UI has to be built in code like in the DOS days. Seriously? No GUI builders? In 2010? Sheesh. Now to the tip of today: How to make a folder on a SD Card.

Sounds simple right? Open a FileConnection, call a mkdir() method? Nope. IO Exceptions, Cannot access root file system and other not very helpful messages (all that is in addition to the fact that you cannot work with both debugger and SD card at the same time. very convenient).

Basically, the hidden gem is – you must have a trailing slash at the end of your path. Otherwise – no mkdir() for you!