This file's purpose is to guide contributors and developers to help on the
digiKam project.

========================================================================
10 golden rules for starting with open source
========================================================================

Before to contribute to digiKam project, please take a look to this link:

http://schlitt.info/applications/blog/index.php?/archives/541-10-golden-rules-for-starting-with-open-source.html

========================================================================
Source code formatting:
========================================================================

Adhere to this style guide strictly while adding new code to digiKam or
working on existing code.

-------------------------------------------------------------------------
* Indentation length
-------------------------------------------------------------------------

Indent with 4 spaces exactly.

for eg:

void function()
{
....int a;                    // 4 spaces from beginning
....for (int i=0; i<10; i++)  // 4 spaces from beginning
....{                         // 4 spaces from beginning
........a = i;                // 4 spaces from previous indent block

Emacs by default will indent to 4 spaces
vim users add this to you .vimrc
 set tabstop=4

-------------------------------------------------------------------------
* Tabs vs Spaces
-------------------------------------------------------------------------

Absolutely no tabs. Use a sensible editor which will convert tabs to spaces.
This will reduce unnecessary changes in your cvs commits.

Emacs by default will convert tab to spaces.
For vim users, add this to your .vimrc
  set expandtab

-------------------------------------------------------------------------
* Line length
-------------------------------------------------------------------------

Line length should never exceed 80 chars (unless really necessary - these
cases are rare). Having long lines greatly reduces readability of code

-------------------------------------------------------------------------
* Bracketing
-------------------------------------------------------------------------

In all cases, {} brackets should start on a newline and should be
aligned with previous line (follow the indentation spaces). for eg.

class A
{ //new line
...

for (int i=0; i<10; i++)
{ //new line

if (a == foobar)
{ //new line
...
}
else
{ // new line
..
}

-------------------------------------------------------------------------
* Positioning of Access modifiers
-------------------------------------------------------------------------

public, private, protected, public slots, ... should be aligned to the
beginning of the line with no margin

class A
{
public: // aligned to left
...
private Q_SLOTS: // aligned to left


Follow a consistent order in defining these attributes. The recommended
order is public, protected (functions), private (functions),
signals, public slots, protected slots, private slots, private (variables)

========================================================================
Class, file and Variable names:
========================================================================

-------------------------------------------------------------------------
* Class and filenames
-------------------------------------------------------------------------

- filenames should always be in lower-case
- class names should match the filenames. Capitalize the first letter and
  other letters logically to improve readability

-------------------------------------------------------------------------
* Protected Member variables
-------------------------------------------------------------------------

- protected member variable names should always be of the form m_varName.
- Capitalize logically so that it becomes easy to read it. Do not capitalize
  the first letter after _ (Use m_varName not m_VarName)
- variable names should be indicative of their functionality and also of
  the type they belong too if they are instances of qt widgets.
  for eg, QCheckBox* m_autoRotateCheckBox;

-------------------------------------------------------------------------
* Non-Member variables
-------------------------------------------------------------------------

- non-member variables should follow the same naming convention as the member
  variables, except for the leading m_

-------------------------------------------------------------------------
* Private Member variables
-------------------------------------------------------------------------

- private member variables must be stored in a d private container to reduce
  compilation time and improve binary compatibility between digiKam components.
  See more information how to use a 'd' private class at this url:

  http://developer.kde.org/policies/librarypolicy.html

========================================================================
Comments and Whitespace
========================================================================

Use whitespaces liberally to improve readability. Add blank lines between logical
sections of the code.

Comment as much as possible. Position comments at the beginning of the
section/line you want to comment, NEVER at the end of the line

// put your comments here
a = (b == foobar) ? 1 : -1;

a = (b == foobar) ? 1 : -1; // you are asking for trouble by putting comments here


========================================================================
Header files
========================================================================

- Add copyright to top of every file. Use the same header than others digiKam
  source code.
- Double inclusion protection defines are all upper case letters and are
  composed of the classname and a H suffix separated by underscore

#ifndef ANOTHERNICECLASS_H
#define ANOTHERNICECLASS_H

class AnotherNiceClass
{
...
}

#endif

- Use forward declarations as much as possible.

class QFileInfo;

class A
{
....QFileInfo* m_fileInfo;


========================================================================
Automatic source code formatting
========================================================================

The above coding style guidelines can be automatically applied with
astyle (http://astyle.sourceforge.net/).

Run it in the directory where the files are located that should be formatted
with the following command:

astyle --brackets=break --indent=spaces=4 --convert-tabs --indent-switches \
       --break-blocks --break-closing-brackets --pad-header                \
       --align-pointer=type --indent-col1-comments --add-brackets          \
       --min-conditional-indent=0                                          \
       `find -type f -name '*.cpp'` `find -type f -name '*.cc'` `find -type f -name '*.h'`

Another way to apply the coding guidelines with astyle is to use the
fileformatter script in /project/scripts. This script will also clean up the
source tree and remove backup files that had been created by astyle, 
if the appropriate command line argument is given.

========================================================================

To handle the command easier, create a bash function in ~/.bashrc, e.g.

digikamformatcode()
{
    astyle --brackets=break --indent=spaces=4 --convert-tabs --indent-switches  \
        --break-blocks --break-closing-brackets --pad-header \
        --align-pointer=type --indent-col1-comments --add-brackets \
        --min-conditional-indent=0 \
        `find $1 -type f -name '*.cpp'` `find $1 -type f -name '*.cc'` `find $1 -type f -name '*.h'`
}

You can pass a parameter to the function, in this case the first parameter is the directory, where
files should be formatted.

Examples:

1. Run astyle in the current directory
$> digikamformatcode

2. Run astyle in a different directory
$> digikamformatcode /home/user/code/svn/digikam/


========================================================================
General recommendations
========================================================================

Please take a look into KDE contrib page tips before to write code/patches for
digiKam project : http://techbase.kde.org/Contribute

Use the same .cpp/.h header than the rest of digiKam project.

Use a decent editor which does auto-indentation/syntax-highlighting for you.
I personally use Kate (Gilles).
There are excellent initializer scripts in the kdesdk
package for xemacs and vim which can substantially increase your productivity.

Just to give a taste of what i can do with emacs (and kdesdk):

* automatically insert copyright (and ifdefs) in new files.
* insertion of class function definitions for declared class
  functions in header with one keystroke
* switch between header and declaration files with one keystroke
* go to corresponding definition/declaration with one keystroke
* tab completion of variable/function names already declared.

========================================================================
GDB Backtrace
========================================================================

If you found a context to crash digiKam, you can provide a backtrace using GDB debugger.
digiKam need to be compiled with all debug info else the backtrace will not suitable.
There is a configure option for that:

$> cmake . -DCMAKE_BUILD_TYPE=debugfull
$> make
$> su
$> make install/fast

To make a backtrace with GDB use following command:

$> gdb digikam
> run
> ...
> _crash here_
> ...
> bt
> _the backtrace is here_
> quit

Post this backtrace at the right place (KDE Bugzilla or developement mailing list) for investigations by developers.

For Windows users, take a look on this tutorial :

http://techbase.kde.org/Development/Tutorials/Debugging/Debugging_on_MS_Windows

========================================================================
Memory leak
========================================================================

To check any memory leak problem in digiKam, valgrind is your friend (http://valgrind.org)
Try this command line to use with valgrind :

valgrind --tool=memcheck --leak-check=full --error-limit=no --suppressions=project/digikam.supp digikam

NOTE: digikam.supp file is available in digikam/project sub-folder.

========================================================================
Profiling with cachegrind
========================================================================

Valgrind also includes a tool to find out in which parts of your code time is spent.

valgrind --tool=callgrind digikam

Profiling can be disabled at startup to limit the output to the code you are interested in.
Start with

valgrind --tool=callgrind --instr-atstart=no digikam

and prepare the situation you want to profile. Then, in another console, start profiling with
"callgrind_control -i on" and, after the situation has passed, request a profile dump with
"callgrind_control -d".
The resulting callgrind.out files need to be viewed with the kcachegrind program, e.g.:

kcachegrind callgrind.out.16693.1

========================================================================
Unit Testing / Automated Testing
========================================================================

Unit Testing is great way to ensure that software units (in OOP this normally
means classes) work as expected. Wikipedia gives a good introduction to
Unit Testing:
http://en.wikipedia.org/wiki/Unit_testing
It is also worth to follow most of these rules while writing unit tests:
http://geosoft.no/development/unittesting.html

The digiKam test suite is located under tests and will be compiled if
KDE4_BUILD_TESTS is set to ON in the CMake call. After compiling the source code
the tests can be executed via

make test

The console output while running the tests is stored in
Testing/Temporary/LastTest.log
in the CMake binary dir.

All tests are simple binaries that can be executed separately if needed.

To see kdebug messages during testing you need to setup kdebug once again for a
special testing environment used while running the unit tests. This can be done
via

KDEHOME=$HOME/.kde-unit-test kdebugdialog

=========================================================================
Checking for corrupt Qt signal slot connection
=========================================================================

Use this alias for running digikam:

alias digikamdbg="digikam 2>&1 | tee - /tmp/digikam.out; echo -e \"\n\n\nPossible connection errors:\n\n\"; cat /tmp/digikam.out | grep -A2 'Object::connect'"

It will print a list of connection warnings after terminating the program.
Moreover the complete console log of the last session is stored in
/tmp/digikam.out.

=========================================================================
Finding duplicated code
=========================================================================

Code duplication should be avoided as bugs have to be fixed for every piece of
duplicated code. The current duplication can be analyzed eg. with Simian:
http://www.redhillconsulting.com.au/products/simian/

In the digikam checkout directory run:
java -jar simian.jar `find . -regex '.*\.cpp\|.*\.h' | grep -v 3rdparty`

This prints out a list of duplicated code segments.

=================================================================================
API Documentation Validation, User Documentation Validation, Source Code Checking
=================================================================================

The following site check on a daily basis for the a.m. errors:
www.englishbreakfastnetwork.org/krazy/

It can be very useful, in particular before major releases.
Don't trust it blindly! Sometimes they propose too advanced modifications that
are no compatible with the prevailant include files.

========================================================================
Usability issues
========================================================================

OpenUsability project has define default menu structure and keyboard shortcuts:

http://wiki.openusability.org/guidelines/index.php/Appendices:Keyboard_Shortcuts

========================================================================
Generate API documentation
========================================================================

To generate API documentation, you need to install:

- Doxygen program (http://www.doxygen.org)
- Dot program (http://www.graphviz.org)

After cmake generated a Makefile you can call 'make doc'. A new subfolder named
'api' will be create in the program's binary dir.
Warning, this can take a while.

For finding documentation errors, doxygen generates a warning log file at the
cmake binary dir called 'doxygen-warn.log'.

========================================================================
Speed up the code-compile-test cycle
========================================================================

Assuming you have setup your environment in ~/.bashrc as is suggested for KDE4 development,
you can add something like this to your ~/.bashrc:

function digikam_start
{
LD_LIBRARY_PATH=${KDE_BUILD}/extragear/graphics/lib:${LD_LIBRARY_PATH} ${KDE_BUILD}/extragear/graphics/digikam/digikam/digikam
}

function digikam_start_gdb
{
LD_LIBRARY_PATH=${KDE_BUILD}/extragear/graphics/lib:${LD_LIBRARY_PATH}  gdb ${KDE_BUILD}/extragear/graphics/digikam/digikam/digikam
}

This allows you to run digikam after compiling without the need of a "make install", even
if you changed code in the libraries.

========================================================================
Working with branches from Subversion repository
========================================================================

To implement new feature it usual to use a dedicated developement branche from svn and let the
trunk code as stable for production. To synchronize branche with trunk, use these commands into your
local branch checkout:

$> svn merge -r_COMMIT_ID_:HEAD svn+ssh://_YOUR_ACCOUNT_@svn.kde.org/home/kde/trunk/extragear/graphics/digikam

_COMMIT_ID_    is the last revision from trunk which have been synchronized to your development branche.
_YOUR_ACCOUNT_ is your subversion login name.

========================================================================
Working with branches from Git repository
========================================================================

As with Subversion, it usual to use a dedicated developement branche from Git and let the
master code as stable for production. To synchronize branche with master, use these commands into your
local branch checkout:

$>git checkout master
$>git pull --rebase
$>git checkout -b MY_DEVEL_BRANCH GIT_REMOTE_PATH
  Branch 'MY_DEVEL_BRANCH' set up to track remote branch path 'GIT_REMOTE_PATH' from origin.
  To list GIT_REMOTE_PATH, use 'git branch -a' command
  Switched to a new branch 'MY_DEVEL_BRANCH'
$>git merge master
  Merge made by the 'recursive' strategy.
  ...
$>git push
  ...

The 2 first lines take a sure that your local master repository is up to date. The 3rd line create in local the "MY_DEVEL_BRANCH"
development branch. If you have already created this branch, just run "git checkout MY_DEVEL_BRANCH". 
Merge between master and "MY_DEVEL_BRANCH" branch is done with 4th line. Git can ask you to resolve conflicts here.
When it's done, it will ask you a commit comment. Finally push your merge into remote KDE repository.
