check the power source

fsuperga:
The issue is how to understand which power source is actually connected.
Reading this schematic : http://arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf I think the best way is to connect USBVCC pin directly (or passing through a diode) to a digital pin, for example pin 7.
So, reading the digital state of this pin I can understand what kind of power source is providing energy.

Yes, you can do that. I suggest you use a series resistor of at least 10K between USBVCC and the pin, to protect the input pin in case the USB voltage is a little higher than the regulator voltage.