Archive | Solution RSS feed for this section

Fix: Incoming Skype messages stealing focus on Linux Mint (Cinnamon)

25 Sep

After upgrading my PCs to Linux Mint Debian Edition, and thereby using the Cinnamon user interfacen, I encountered a particularly bothersome behaviour: For every incoming Skype message the skype window would come to the front and grab focus. So while typing you would even  continue to type in the Skype Window. Luckily I found a fix in the Skype Linux support forums.

This behaviour is already fixed in the Cinnamon Repository, But you can fix it yourself and don’t have to wait for an update. For instance the problem doesn’t seem to be fixed in the Update Pack 5 to Linux Mint Debian Edition.

Solution

At first locate the file called windowAttentionHandler.js – this can easily be done on the command line:

locate windowAttentionHandler.js

For me the path is /usr/share/cinnamon/js/ui/windowAttentionHandler.js – now you just need to open this file as a super user and make the same change as in the commit.

More easily speaking, at first open the file in a simple editor (pluma, gedit, nano, vi…) as the super user:

sudo gedit /usr/share/cinnamon/js/ui/windowAttentionHandler.js

now find this line:

if (!window || window.has_focus() || window.is_skip_taskbar())

And replace it with this line (or just adjust the latter part so it lookes like the line below):

if (!window || window.has_focus() || window.is_skip_taskbar() || window.get_wm_class() == "Skype")

For some weird reason the line wrapping of the code block doesn’t work. So hover that code box with your mouse and select view source code, you can then copy and paste from there!

Please be careful not to mess with the file, that could break your system (or at least the handling of the attention of windows). So make sure to make a backup of that file.

After this a restart is probably required for the changes to take effect, at least it was for me. As commenter Clement said you can also just hit Alt + F2 and type “r” and hit ENTERin order to restart cinnamon (Thanks for the comment!). So now, happy using Skype ;-)

(By the way: there is a new Skype version for Linux – which doesn’t seem to be in the repositories yet. Check it out!)

Permanently deactivating a network adapter in Linux

14 Sep

So my PC has 2 wireless adapters. One crappy internal one and a good external one which I bought because the internal one is so bad. But on system start I now had the problem that the crappy wifi card always wanted to connect which I didn’t want it to do. So I had to go through the trouble of running the following command in my shell:

sudo ifconfig wlan0 down

mostly a few times, since it didn’t seem to pick it up from the beginning. I tried to run this command at system startup using rc.local as described here, but that didn’t work for some reason. But there is a different solution, that worked for me!

Solution

Blacklist the kernel module that is responsible for running the wifi adapter. In other words, don’t load the Linux driver for the bad wifi adapter so it won’t work. You can do this the following way:

At first you need to find out which kernel module is responsible for the wireless card you want to deactivate. The following command shows you all currently loaded kernel modules:

sudo lsmod

I grepped this list for part of the name of the chipset of my wireless card, which is “RTL8185″ so I went with:

sudo lsmod | grep rtl81

There the “rtl8180″ kernel module showed up as being loaded, with no other modules using it. Now we can try to remove this module from the currently loaded modules with

sudo modprobe -r rtl8180

If everything worked, the unwanted wifi card should just have disappeared from your network control panel. It also shouldn’t show up any more when running the “ifconfig” command. If not, you probably removed the wrong module in that case you should rather add that Linux kernel module back in:

sudo modprobe -a removed_module_name

However if it worked and the wireless adapter really disappeared, then we don’t want to remove it every time we boot our system. That should be done automatically! And there indeed is a mechanism for that. There is a blacklist of modules that shouldn’t be loaded on system boot. In order to use this you have to modify /etc/modprobe.d/blacklist (or create it, as I had to) with a text editor and admin rights. So I did:

sudo nano /etc/modprobe.d/blacklist

And modified the file to look like this:

blacklist rtl8180

Of course you have to modify my module name with yours. This will blacklist the module/driver automatically on system boot/startup. This worked beautifully for me on my Linux Mint Debian Testing operating system. I hope it works for you as well. Of course this technique is more general and can be used to blacklist any kernel module/driver.

Setting up PostgreSQL for Ruby on Rails on Linux

12 Sep

So every once upon a time I run into the situation, that I have a newly set up machine and have to configure my system again to use PostgreSQL and play nicely with Ruby on Rails. And I don’t want to have to google the set up every time, hence this post (and of course to help people with similar problems). Why PostgreSQL? Well many people like it and you need it for instance for deploying on heroku and your development environment should be as close to your production environment as possible.

What I do here is a way that works for me on my Linux Mint Debian Testing machines. Be aware that this is my development set up – I don’t use this in production.  I’m no PostgreSQL expert so, this just works for me and I hope that it will for other people as well :-) . Suggestions/Improvements are welcome as always.

Let’s get started! So at first we have to install PostgreSQL:


sudo apt-get install postgresql

After we’ve don this we need to create a user in PostgreSQL. So we use the user account of PostgreSQL to create a user with sufficient rights. I just take my own account and grant it sufficient rights with PostgreSQL. Don’t forget to substitute my username with yours!

tobi@speedy ~ $ sudo su postgres
[sudo] password for tobi:
postgres@speedy /home/tobi $ createuser tobi(substitute with your username)
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n

At this point it would be good to install the pg gem – which is the adapter for the PostgreSQL database. So simply add

gem 'pg'

to your Gemfile. Also make sure to replace existing database gems – like sqlite. Now run:

bundle install

to update your dependencies. That should go smoothly, otherwise you are probably missing dependencies.

Now you still need to modify your config/database.yml. Most of all you need to change the adapter to “postgresql”. Here is the development part of my database.yml for reference:

development:
  adapter: postgresql
  database: ArbitaryDatabaseName
  pool: 5
  timeout: 5000

Make sure that the different environments (development, test and production) have got different databases (meaning the database property should be different). Otherwise they will influence each other and you don’t want that.

In order to finish the set up you need to create all the databases, which should work flawlessly by now:

rake db:create:all

When this is done you should be able to “rake db:migrate” your database as you are used to (and be sure to do so).

I hope this post helped you with setting up PostgreSQL for Ruby on Rails on Linux. If something didn’t work for you, you feel that a step is missing or you just have a useful tip – please feel free to comment!

Automatically compile CoffeeScript on Save with gedit

3 Jun

CoffeeScript is a nice layer on top of JavaScript that takes away quite many of JavaScripts quirks and rough edges. I really like it as it makes development much more enjoyable to me. It compiles directly to JavaScript which is awesome for compatibility reasons. However that means whenever you change a file and want to test the changes you have to recompile the file. Some editors do this automatically. gedit however doesn’t have CoffeeScript support out of the box.

That’s relatively easy to fix however. At first you should go ahead and install syntax highlighting for CoffeeScript. So now we got some nice syntax highliting, looks better right?

The auto compile problem is also easy to fix. gedit comes with the External Tools plugin which makes it fairly easy to fix this when CoffeeScript is already installed properly (for installation information click here).

At first you need to activate the External Tools plugin. Go to Edit->Preferences and then select the Plugins tab. It should look like this:

External Tools in gedit

The plugins menu in the gedit preferences

Make sure that the box before External Tools is checked. Now you can go to Tools->Manage External Tools… Click on the + on the bottom left in order to add a new tool. Name it however you like and insert the following in the code box:


#!/bin/sh
coffee -c $GEDIT_CURRENT_DOCUMENT_PATH

Now you can choose whatever shortcut you would like for this command. If you make sure to set the applicability to CoffeeScript files only and set Save to “Current Document”, then you can even set the shortcut to Ctrl + S. That works beautifully for me.  The option to set the applicability to CoffeeScript only might only be available after installing the syntax highlighting for gedit mentioned earlier. Here’s what my settings for my compile CoffeeScript tool look like:

External Tools Settings

The External Tools Settings for making CoffeeScript compile automatically on save

This should do it. If it doesn’t work, feel free to leave a comment so we can sort that out :-)

Happy coding everyone!

PS: This should also work with pluma, a fork of the old gedit editor from the MATE project

Getting the “fortune cookies” back in the Linux Mint Debian Edition terminal

13 May

Some weeks ago I installed Linux Mint Debian edition after being a loyal and happy user of the Linux Mint main edition for some years. It was a very nice experience but something was missing… in the main edition, every time you open a terminal you are greeted by an animal, which has something more or less funny to say. I always liked that, it’s part of Linux Mint for me. However when I opened the terminal in my freshly installed Linux Mint Debian Edition I saw the following:

empty_terminal

No one greeted me. So I decided to ask on the forums. Gladly there is a solution for this (thanks to äxl for the answer!). You can simply run:


gconftool -s -t bol /desktop/linuxmint/terminal/show_fortunes true

Alternatively you can open the graphical configuration tool with “gconf-editor”, navigate to that path and change the value by hand. And then there they are again, my beloved “funny greeting messages”, “fortune cookies” or whatever you want to call them. And notice what really important wisdom my terminal has to share with me this time:

funny terminal

Autostarting applications in Linux Mint Debian Edition

17 Apr

A few days ago I finally made the step to switch from Linux Mint main edition (Linux Mint 10 was getting old) to the all new Linux Mint Debian Edition Release Candidate with the new Cinnamon desktop. It’s been great so far.

However I was really missing a feature of the main edition. There you could simply right click on a menu entry and say “Launch on startup”, which has been the most convenient way to add an autostart that I’ve ever seen. Browsing the settings and the web I at first didn’t find a way to autostart applications. I found lots of descriptions involving files and directories that don’t seem to exist in my Linux Mint Debian Edition. Well enough babbling.

Solution

Simply run:


gnome-session-properties

You can do this in the terminal or with Alt + F2 (gnome do). There you have a list of all your startup applications and you may add applications by specifying their command (like: “thunderbird” or “firefox”) but you may also remove startup applications. This looks something like this:

This should work with all Gnome based desktops (Gnome 2, Gnome 3, Mate, Cinnamon), I haven’t tested it though. It’s fairly easy but embarrassingly took me long enough to figure out, so I figured that it’s better to blog about it and maybe save somebody else some time.

Setting up PostgreSQL with TravisCI

1 Feb

TravisCI is an awesome free continuous integration system that just takes your github repositories and then runs all your tests – it is pure awesomeness and easy as cake.

However when testing a web application you also have to setup a database – they have got good docs for that, but still I ran into problems.

What was my problem? In their description the database name is “myapp_test” and I believed that it would not matter and it could be anything like “Tracketytrack Test” I wanted. I got proven wrong. Apparently it has to be “myapp_test”. Also for some reason I had to add a manual call to “rake db:migrate” – I may check into this later. For your reference, here the relevant parts of my .travis.yml as a gist.

And here they are as well for your reference:

postgres:
  adapter: postgresql
  database: myapp_test
  username: postgres
before_script:
  - "psql -c 'create database myapp_test;' -U postgres"
  - "rake db:migrate"

Never ever modify objects used as hash keys

22 Dec

Before we get to the real topic and the source of some quite big headaches for me, I want to explain something first.

How can I use my own objects as keys in a hash (dictionary)?

You simply have to define the eql? and hash methods of those objects in some meaningful way so that they reflect the equality you want to achieve in the hash (dictionary). Please don’t be confused by the double use of the word hash. The method computes a number that should be unique to the object. Or put it in another way, if two objects return the same they are considered to be the same. Think MD5 or SHA1.

The hash as a construct is more like a dictionary, mapping from a key to a value.

Problem

Now to the real problem. I had a finite number of external representations for something. In this example let’s take the coordinates of cities (the real example was different, but I don’t want to spoil the test taken at ThoughWorks). The coordinates are the internal representation whereas symbols like :city_a are the external representation.  In the beginning I had to start with the external representation, convert it into an internal representation for computation and in the end I had to output the external representation once again. So I thought that it would be nice to have a one-to-one mapping instead of case statements and possibly duplicated code.

So my idea was to go with a hash and thanks to the hash#invert method I could get both the internal and the external representation. So I went ahead and wrote that hash (this was the actual cause for my former blog post) and the corresponding methods:

(sorry for the lack of indention, wordpress makes that awfull hard on me)

class City

def initialize x, y
@x = x
@y = y
end

CITIES_COORDINATES = { city_a: City.new(10, 5),
city_b: City.new(15, 25),
city_c: City.new(30, 3),
city_d: City.new(40, 12) }

def self.for symbol
CITIES_COORDINATES[symbol]
end

def to_sym
CITIES_COORDINATES.invert[self]
end

# stuff omitted

end</pre>

Everything good so far. So where is the problem?

The problem was that 2 of my tests started going nuts for real. When I instantiated a new object and said that it was facing North it claimed to be facing East. I thought that maybe my before :each wasn’t working properly (as the tests involved changing the direction) but I checked and the two objects indeed had different object_ids. So they weren’t the same object but the tests seemed to influence each other – somehow. I could comment out all of the tests but the 2 failing ones and they would magically pass. Also running each of them separately would let them pass.

Solution

The problem essentially was that in a City object, there was a method changing the object (e.g. not immutable). However this doesn’t seem to play too well with using those objects as hash keys. I changed the method to instead return a new object representing the new city. After that everything worked as expected.

Don’t use the constructor before initialize is defined

18 Dec

Problem

If for some reason, most likely convenience, you want to have a constant containing an instance of an object in your class you have to pay attention to where you define this constant. An example might be that instead of City.new(10, 5) you would want to refer to it as the constant CITY_A.

(Please be aware that I adjusted the example a bit since I didn’t want to give away information about the ThoughtWorks coding test, it made more sense in its original application but just run with it.)

Fair enough so let’s give it a shot:

class City

CITY_A = City.new 10, 5

def initialize x, y
@x = x
@y = y
end

# rest of the class omitted

end

Now awkwardly enough when we try to let ruby interpret this code we get the following error (the error looks different depending on your interpreter, this is Ruby 1.9.3 roughly the same happens with JRuby and Rubinius):

city.rb:3:in `initialize': wrong number of arguments(2 for 0) (ArgumentError)
from city.rb:3:in `new'
from city.rb:3:in `<class:City>'
from city.rb:1:in `<main>'

But we have an initialize method and it takes 2 arguments! So why does the Ruby interpreter claim that it doesn’t take arguments at all?

Solution

So what’s wrong? Well we have to know how the ruby interpreter works. It starts at the top of the class and there it doesn’t yet know that we have an all new initialize method, taking 2 parameters. So fixing this is quite easy, here is the fixed class:

class City

def initialize x, y
@x = x
@y = y
end

CITY_A = City.new 10, 5

# rest of the class omitted

end

As you can see the constant declaration was moved beneath the declaration of the initialize method. While I prefer to define constants at the very top of my classes, this is the only method (known to me) to make this code work.

On a side note, this is a simplified example my actual use for class instances in a constant was a bit more complex and an adjusted version may be seen in my next blog post.

Regexes: non-greedy and . matching whitespaces – Rubular to the rescue!

2 Oct

So some weeks ago (unfortunately I didn’t really have the time to post this immediately)  I ran into the following problem, I wanted to match the following snippet:

Shoes.setup do
gem 'twitter'
gem 'oauth'
gem 'launchy'
gem 'feedzirra'
end

I wanted to do this in order to transform a (red) Shoes application to a green shoes application (where the gems are handled by ruby gems as you are used to, as it uses a normal ruby interpreter). So how do you match this with a regex? I ran into 2 little problems.

First: I wanted to match Shoes.setup do – end, and everything in between. Unfortunately “.” just matches any single character, no whitespaces. After a bit of google action I found out that the m-switch exists to alter this behavior for the regex. So there we go…

But what is this? It replaced the whole file! How could that be? Oh yeah, underneath there is a whole program with its own “end” – and regexes are by default greedy and try to match as many characters as possible. So everything in between Shoes.setup do (the first thing in the program) and the last end… so how to make a matcher non greedy? Just add a question mark (?) after the matcher!

Solution

So in the end the regex looks like this:

/Shoes\.setup do.*?end/m

I could have known this way faster if I would have just stick with my favorite Ruby Regex reference, Rubular! It has an amazing short reference and now is also linked at the Resources page.

Follow

Get every new post delivered to your Inbox.

Join 356 other followers