Reflections of a scattered Mind!!

"Not all scattered pieces are of broken glass"

Firefox Addons: Little Housekeeping Tips

I’m working on a Firefox addon in the last few weeks and this is my 1st experience with Firefox addons. During my tiny experience, I came across some of those little things that helps you when you’re in trouble & here I’m writing about those simple tips. Of course, I mixed some changes in Gecko 2.0 also along with these….

I know these are very basic, and almost everyone who worked on the addons might be knowing them, but hey, didn’t I mention you that this is my first encounter with building firefox addon?

How can I be sure if a Binary XPCOM component is loaded / attempted to load from Firefox?

Close all Firefox windows and run the following from command line:

1
2
3
set NSPR_LOG_MODULES=all:5  
set NSPR_LOG_FILE=c:\ff.log  
C:\Program Files\Mozilla Firefox\firefox.exe  

What does it do? Line 1 sets the modules to load & the log level. In the above commands, we are asking to log all modules with log level 5 (meaning all logs). Line 2 sets the path to log file. Now, just launch the Firefox from command line (so that the variables are set)

Troubleshooting Tip: Do not use quotes around the log file name, like set NSPR_LOG_FILE="C:\ff.log". I tried it the 1st time and it doesn’t work.

References: Troubleshooting XPCOM components registration, NSPR Logging

How can I sure if an XPCOM component is successfully registered?

This works for both binary & JS components.

Enable Chrome errors in Error Console, if you haven’t done already! To enable, set javascript.options.showInConsole to true in about:config preferences.

Open Error Console (Ctrl+Shift+J)

Evaluate Components.classes["components-contract-id"].name. Use with the components’ contract id.

If it gives no error, the component is successfully registered.

References:  javascript.options.showInConsole, Troubleshooting XPCOM components registration

How to log errors from the XPCOM components?

This is simple thing, and I realized a bit late about it. This is currently how I’m debugging my components (need to see if there is a better way). You can report error messages to the Error Console with:

1
Components.utils.reportError("This is the description of the error");

References: Error Console

nsIConsoleService

The inconvenience with the reportError API is that it logs all messages as errors. To log at various levels (debug / warning / info), we can use the nsIConsoleService. The logStringMessage() function of this service looks very handy.

I haven’t used it yet, so I cannot give any more information on this!

References: nsIConsoleService Reference

UnPack option

If you are using binary components in the addon, you MUST set the unpack value to true in install.rdf file.

References: unpack in Install Manifests

Bootstrap Addons

This feature introduced in Gecko 2.0 answers one of my long-time questions – Why should I restart the browser everytime I add / update / disable / remove an addon?

Though this feature is good, it looks complicated to do all the manual work. I’ve skipped trying this feature for now, but I’ll look into this some other time.

References: Bootstrapped Extensions

Gecko 2.0 XPCOM Changes

Though there are many changes, I’m listing the few which I came across.

  • Javascript component must export NSGetFactory() instead of NSGetModule() function. Binary component must export NSModule() instead of NSGetModule() function. I’ve explained this in detail in the next section.
  • Components in components/ folder are no longer auto-registered & auto-loaded.Components registration will be done through the manifest file.
  • Category Registrations are done through manifest file. Also, category names are modified.
  • You’ll have to use the XULRunner SDK 2.0 for Gecko 2.0

Troubleshooting Tip: Use the sample CPP file (from the references) to get started for binary components. I got a few compilation errors initially, and then I has to add MOZ_NO_MOZALLOC to preprocessor definitions as read somewhere, and it worked..

References: XPCOM Changes in Gecko 2.0, nsSampleModule.cpp source code

Component Registration in Firefox 4.0

All the components to be registered has to mentioned in the chrome.manifest file.

  • All the interfaces (xpt files) are to be registered using interfaces command.
  • A JS component must use component command. It needs to mention its class id also. The contract id can be assigned to this JS component by a separate contract command.
  • A binary component can be registered with binary-component command. A binary component registration need not specify any class-id or contract-id in the manifest file, since they are available through the exported NSModule() function.

References: Chrome Registration

If you have felt all the content is copied from somewhere, its ok because its true. I’ve learned many things from MDC itself, I’m just aggregating those things here!!

If I’ve got something wrong in here, please do mention them in the comments as I have to correct them in the post as well as in my mind…

Quotes I Like:: Vol. III

Some random quotes / one-liners, I liked.

  • Great minds are always feared by lesser minds
  • … there’s a gap between what science knows and what business does (Dan Pink)
  • All those who wander are not lost.
  • What we have done for ourselves alone dies with us; What we do for others dies with them. What we do for the world is too insignificant to count.
  • Even if an analog clock is broken, it shows correct time at least twice a day.
  • A happy man is not the one who has everything, but who makes most of everything he has.
  • Keep your face to sunshine and you can’t see the shadows.
  • Make sure the thing you’re living for is worth dying for.
  • Speaking without egos, Loving without intentions, Caring without expectations, Praying without selfishness is the sign of True Relation
  • I decided to send you the cutest n sweetest gift of the world. But the postman shouted at me sayin - Get out of the postbox :-)
  • If you don’t count any of my failures, I’m quite successful.
  • To live is the rarest thing in the world. Most people exist, that is all.
  • The biggest fools are those who seek an interpretation for everything
  • You’ll never have a quiet world till you knock the patriotism out of the human race

You can find my previous favorite one-liners posts at: Quotes I like:: Vol. II & Quotes I like:: Vol. I

Debug PHP: Setup

Okay! I forgot how to set up debug for my PHP, so I had trouble today trying to set it up today and had to spend about an hour to do so… So, I decided I should note it down for future reference and hence I’m here now!

Tools: PHPEclipse, PHP 5.3.3, XDebug

  1. Make a PHP file with content as <?php echo phpinfo(); ?> and browse the page on localhost.
  2. Copy the source of the above page from the browser and paste it in http://xdebug.org/find-binary.php and click “Analyze my phpinfo() output”. This step will give you tailored instructions on how to setup, especially points to the right DLL to use.
  3. Be careful with the editing php.ini file. Prefer to add the zend_extension line at the top of the file (It doesn’t work if its the last line).
  4. And start debugging from the Eclipse IDE !!

C++ Quickies #I : Long Live the STL

Its been quite some time (16+ months) I’ve been out of college and started using STL. Trust me, I didn’t know much about STL in college, though I used std::string & std::vector everywhere I could. Now, it seems like I couldn’t (wouldn’t ?) write a program without using STL at all. A few snippets that I really found useful (and simple) and I regularly use where ever possible.

To output the elements (all / subset) in a container (lets say vector), separated by a delimiter, say new line:

1
2
#include <algorithm>
std::copy(v.begin(), v.end(), std::ostream_iterator(std::cout, "\n"));

Want to sort an array? No problem … :)

1
2
#include <algorithm>
std::sort(array, array + lengthOfArray);

Read from the standard input into a vector until the end-of-stream?

1
2
3
4
5
6
7
8
9
10
#include <algorithm>
#include <iterator>
#include <vector>

typedef std::istream_iterator<int> istream_iterator_int;
std::vector<int> v;
istream_iterator_int start (std::cin);
istream_iterator_int end;
std::back_insert_iterator<vector<int> > dest (v);
std::copy (start, end, dest);

Count the no. of instances an object is found?

1
2
#include <algorithm>
size_t count = std::count(v.begin(), v.end(), 42); // Returns no. of elements in v with value 42.

And let me inform you… This is just a grain of what STL can do…. :)

References:

http://www.cs.brown.edu/~jak/proglang/cpp/stltut/tut.html

http://www.cplusplus.com/reference/algorithm/

Just Friends! It Got Something !!

If you’re wondering what it is, ‘Just Friends’ is a novel by an Indian writer. I think it is his debut novel, but don’t take my word on this. You need to google (or bing, for Bing fans) it to be sure. By ‘It got something’, I don’t mean to say, its an awesome book. I wouldn’t even say its a good book, its just another book (add some 18+ masala content to the recipe) . What impressed me in the book are the quotes (quoted by the author himself, I believe) in it. They are not inspirational or something, but I liked them (not that I like only inspirational, in general). So, I decided to put my favorites here and share with you.

  • Sleep is deadly and love is lethal. The combination is so powerful that it leaves you helpless, forcing you to sleep later.
  • A sleepless night is a lover’s right
  • Restless nights, drowsy mornings, confused afternoons, lonely evenings…
  • Eyes are like prostitutes - they reveal more than they can conceal.
  • I laughed, she laughed. Problem solved.
  • I smiled, she smiled. All problems could wait.
  • You pose a great competition to sugar for its characteristics of deadly sweetness and mixing quickly and easily in unknown waters.
  • I wasn’t superstitious. It was just… love.

In case they don’t make sense to you, you need to read the novel then coz they all are situational, though you can identify the situations from the statements itself for many of them.

Until next post, have a peaceful life (great would be great, though)…

Learning Android: Part I

I will use this post to talk about my ‘Getting familiar with Android’ learning. As announced in the last post, I’ve started learning Android, but not even at half-pace as I wanted to.

Anyways, let me tell what I’m doing in learning. I’m using ADT on Eclipse Helios and using the Emulator for testing purposes. Currently focusing on the samples present along with Android SDK. Gone through the Application Fundamentals available along with the SDK.

Interesting Things:

  • Most importantly, its good to see myself learning the things I’m interested in from the start (FYI, I always have that thing for Web & Mobile Development)
  • Very clear & highly helpful documentation (and, also downloadable). And the docs are developer-focused & also lays emphasis on good practices at every step, like i18n, using resources instead of hard-coded strings etc. etc..
  • Separation of layout of the app and the application logic (supports XML files for layout). I get the feeling this is inspired from Flex (mxml & as), but I don’t know.
  • Provides a very good mechanisms for interoperability between various applications. Simply said, I can use the capabilities of other applications installed, and also I can expose my app’s capabilities to other apps. Also, Android search also integrates good with any application’s search.
  • Application Development is done in Java, which is said to be a very developer-friendly. I can utilize this to brush my skills in java, as I have other plans to work in Java in some not-so-far future.

Annoying Things:

  • Tabs are NOT simple to use. I’d prefer a way to declare tabs and its contents in the XML itself.
  • In TabHost, mandatory referencing of elements to’ tabs’ & ‘tabcontent’ doesn’t look like a good programming feature.
  • Missing the tabs & tabcontent id referencing in the TabHost would give no error / warning by Compiler, but fails at Runtime, without any proper information (even while debugging)
  • Emulator takes very long time to start. So, its preferred not to close the emulator after every run. (start along with eclipse and close it before closing eclipse)
  • Emulator keyboard shortcuts - Not too easy to remember (like, Ctrl + F12 for Toggle Portrait / Landscape modes). I’d have preferred if the emulator itself has a help button, to show the shortcuts.
  • Nothing to do with Android, but the bug in Helios (Eclipse Web tools plug-in) has become a pain, forcing me not to us any features while editing resources. Bug - https://bugs.eclipse.org/bugs/show-bug.cgi?id=318108
  • Again, may not be related to ADT (frankly, I don’t know), but the auto-complete context is very slow, taking 30-60 seconds (which is tooooo long, btw). Here again, I’m forced to disable Auto-Activation of context Assist at ‘Window -> Preferences -> Java -> Editor -> Context Assist -> Enable Auto Activation’
  • I  can’t run the application when my focus is on layout files (on resource files, for that matter). I always need to switch to a java file, to run (Ctrl + F11) the application
  • The layout filenames cannot contain uppercase letters. If it contains, the error shown doesn’t help in identifying it

Unclear points for me:

  • In Android XML, for TabHost, @android:id/tabhost works ( i.e; referencing other element with tabhost id ). How can access this element from my Activity class? (since, no reference is created in R.id )
  • Is XML compiled ? Or interpreted at Runtime? If compiled, how can I see the generated Java (?) files? (something similar to -keep compiler option in Flash Builder)
  • And many other things (like, working of Intent, i18n, logging), but these are somethings I need to learn as I dive further in android..

Tasks for coming days:

  • Finish the Tutorials in the resources, and then go to the samples folder available with the SDK.
  • Try to make a couple of simple *usable* apps, so that I get enough practice with the SDK APIs

Also, attaching the projects HelloAndroid.zip and HelloViews.zip I made during my learning.

Wish me that I gain pace in learning Android over the next few weeks…

And, I’m Back!

I’ve decided that I need to celebrate the release of WordPress 3.0. So, I’ve given a thought about the best way to celebrate! And guess what? Using it & spreading the word will the best (both for myself & WordPress too :D). So, I’m back to my blog. And this is not like my previous times !!! I’m going stick around updating my blog for quite some time (a couple of months, for a minimum).

Let me start with what happened during last few months. Actually, many of them I wish they didn’t happen or they happened other way.. :(

  • I tried to get a driving license at Noida, but failed because of lack of local address proof. Seriously, going back to my hometown just for getting a license - doesn’t it sound stupid?
  • With no license, my dream of a getting a bike got wiped away !
  • I’m getting bored (read as vexed, irritated and so on…) of Noida ! What a stupid place it is !! Isn’t it?
  • Laptop is troubling me a lot…. Currently under repair. Wish I get it back soon!
  • Lost all the contacts from the mobile!

And the good part: Google I/O 2010 brought back my interest in Web & Mobile development, due to which, I decided to spend atleast 40 hours in the coming couple of months learning Android. I’ve also decided Learning is going to my major focus area for a couple more years!!

So, wish me luck in putting things into execution !

The Benefits of Failure

This is video of the speech by the famous J.K. Rowling at the Harvard Commencement, 2008.

So, whats in the video?

  • It has some humor in it, especially the first 150 secs. I’m sure a few smiles you’d certainly escape from your tender lips.. :P
  • She talks how ‘being poor’ influences (or atleast attempt to) your life and all in it including career, passion etc. etc.
  • And the core part – The benefits of failure. I think she had put this part in action in a great way, or else, after watching this video, why did I desperately want to fail in a big way despite my fear of failure??

Now that I mentioned I fear failure, let me tell what my mind tells me right now. “You do not fear the failure itself, but your fear its after-effects. Its capable of taking everything fro you !”. But my heart is encouraging me – “When you had done everything from scratch, why do you fear to do it again? It’d easier this time.. !!” :P :P

I know it sounds funny, but really can’t help. Perhaps, this is why people call –

An idle brain is the Devil’s workshop

I Had a Problem

Symptoms:

  • Language: C++
  • Operating System: Windows
  • Target Audience: Programmers
  • Sub Area: Reading ASCII / UTF-8 files from the disk using C++ streams.
  • Trouble: How to handle files whose file names contains non-ASCII characters?? (coz ifstream takes a ASCII file path :–( )

Solution:

  • Use _wfopen_s(), the safe version of _wfopen(), which takes a wide-character file name & gives a FILE pointer i.e; FILE*.
  • Use the obtained FILE* to create a stream, like ifstream in(fp);
  • We used wide-character file name & are reading the file byte-by-byte (actually, its char). DONE !! :–)

BTW, I got help from here! Hope this saves  time for someone out there…

Ask Your Computer to Help You in Household Chores

Does this title come as a surprise to you? But, yes ! computer can help you with some additional things too (apart from computing).

With a small program, you can make the cradle (baby rocker) of your baby swing, without your presence. Shocked? Well, do you recognize you PC has a CDROM and also a program can also open & close a CDROM drive? Continuous opening & closing of CDROM can help in your baby rocker’s harmonic motion.

Check this link for similar innovative ideas: http://blog.makezine.com/archive/2009/08/linux_baby_rocker.html