view mainwindow.cpp @ 76:d575a8f76a53

* Win32 build fixes
author Chris Cannam
date Sat, 20 Nov 2010 10:11:00 +0000
parents 10eb97683aa9
children aaeabc920ca8
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    EasyMercurial

    Based on HgExplorer by Jari Korhonen
    Copyright (c) 2010 Jari Korhonen
    Copyright (c) 2010 Chris Cannam
    Copyright (c) 2010 Queen Mary, University of London
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include <QStringList>
#include <QDir>
#include <QNetworkInterface>
#include <QHostAddress>
#include <QHostInfo>
#include <QDesktopServices>
#include <QStatusBar>
#include <QMessageBox>
#include <QMenuBar>
#include <QApplication>
#include <QToolBar>
#include <QToolButton>
#include <QSettings>

#include "mainwindow.h"
#include "settingsdialog.h"
#include "multichoicedialog.h"
#include "startupdialog.h"
#include "colourset.h"
#include "debug.h"
#include "logparser.h"


MainWindow::MainWindow()
{
    QString wndTitle;

    createActions();
    createMenus();
    createToolBars();
    createStatusBar();

    runner = new HgRunner(this);
    connect(runner, SIGNAL(commandCompleted()),
            this, SLOT(commandCompleted()));
    connect(runner, SIGNAL(commandFailed()),
            this, SLOT(commandFailed()));
    runningAction = ACT_NONE;
    statusBar()->addPermanentWidget(runner);

    setWindowTitle(tr("EasyMercurial"));

    remoteRepoPath = "";
    workFolderPath = "";

    readSettings();

    tabPage = 0;
    justMerged = false;
    hgExp = new HgExpWidget((QWidget *) this, remoteRepoPath, workFolderPath, initialFileTypesBits);
    setCentralWidget(hgExp);

    setUnifiedTitleAndToolBarOnMac(true);
    connectActions();
    enableDisableActions();

    if (firstStart) {
        startupDialog();
    }

    ColourSet *cs = ColourSet::instance();
    cs->clearDefaultNames();
    cs->addDefaultName("");
    cs->addDefaultName(getUserInfo());

    if (workFolderPath == "") {
        open();
    }

    hgPaths();
}


void MainWindow::closeEvent(QCloseEvent *)
{
    writeSettings();
}


QString MainWindow::getUserInfo() const
{
    QSettings settings;
    settings.beginGroup("User Information");
    QString name = settings.value("name", getUserRealName()).toString();
    QString email = settings.value("email", "").toString();

    QString identifier;

    if (email != "") {
	identifier = QString("%1 <%2>").arg(name).arg(email);
    } else {
	identifier = name;
    }

    return identifier;
}

void MainWindow::about()
{
   QMessageBox::about(this, tr("About HgExplorer"),
                      tr("<b>HgExplorer</b> tries to be Mercurial's <b>VSS Explorer:</b> ;-)<br><br>"
                        "-Hides command line in normal use<br>"
                        "-Makes common operations easier<br><br>"
                        "(c) 2010 (lgpl), Jari Korhonen (jtkorhonen@gmail.com)<br><br>"
                        "-Needs Mercurial ;-) (thanks Matt Mackall, Bryan O'Sullivan and others !)<br>"
                        "-Uses excellent Nuvola icons (c) David Vignoni (Thanks, David !)<br>"
                        "-Needs Qt4, mingw (in windows), python, kdiff3 (Thanks to all of you !)<br>"
                        "-Windows standalone install uses hg / python / kdiff3 from TortoiseHg (BIG Thanks !)<br>"
                        "-Windows standalone install uses InstallJammer setup tool (Thanks, great tool !)<br>"));
}


void MainWindow::hgStat()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        
        QString statFlags = hgExp -> getStatFlags();
        if (statFlags.isEmpty())
        {
            params << "stat";
        }
        else
        {
            params << "stat" << "-" + statFlags;
        }
        
        
        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_STAT;
    }
}

void MainWindow::hgPaths()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        params << "paths";
        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_PATHS;
    }
}

void MainWindow::hgHeads()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        params << "heads";

        //on empty repos, "hg heads" will fail, don't care of that.
        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_HEADS;
    }
}

void MainWindow::hgLog()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        params << "log";
        params << "--template";
        params << "id: {rev}:{node|short}\\nauthor: {author}\\nbranch: {branches}\\ntag: {tag}\\ndatetime: {date|isodate}\\ntimestamp: {date|hgdate}\\nage: {date|age}\\nparents: {parents}\\ncomment: {desc|json}\\n\\n";

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_LOG;
    }
}


void MainWindow::hgParents()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        params << "parents";

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_PARENTS;
    }
}



void MainWindow::hgRemove()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        QString currentFile = hgExp -> getCurrentFileListLine();

        if (!currentFile.isEmpty())
        {
            if (QMessageBox::Ok == QMessageBox::warning(this, "Remove file", "Really remove file " + currentFile.mid(2) + "?",
                QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel))
            {
                params << "remove" << "--after" << "--force" << "--" << currentFile.mid(2);   //Jump over status marker characters (e.g "M ")

                runner -> startHgCommand(workFolderPath, params);
                runningAction = ACT_REMOVE;
            }
        }
    }
}

void MainWindow::hgAnnotate()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        QString currentFile = hgExp -> getCurrentFileListLine();

        if (!currentFile.isEmpty())
        {
            params << "annotate" << "--" << currentFile.mid(2);   //Jump over status marker characters (e.g "M ")

            runner -> startHgCommand(workFolderPath, params);
            runningAction = ACT_ANNOTATE;
        }
    }
}


void MainWindow::hgResolveMark()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        QString currentFile = hgExp -> getCurrentFileListLine();

        if (!currentFile.isEmpty())
        {
            params << "resolve" << "--mark" << "--" << currentFile.mid(2);   //Jump over status marker characters (e.g "M ")

            runner -> startHgCommand(workFolderPath, params);
            runningAction = ACT_RESOLVE_MARK;
        }
    }
}



void MainWindow::hgResolveList()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        params << "resolve" << "--list";
        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_RESOLVE_LIST;
    }
}



void MainWindow::hgAdd()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        QString currentFile = hgExp -> getCurrentFileListLine();

        if (areAllSelectedUntracked(hgExp -> workFolderFileList))
        {
            //User wants to add selected file(s)
            params << "add" << "--";

            QList <QListWidgetItem *> selList = hgExp -> workFolderFileList -> selectedItems();

            for (int i = 0; i < selList.size(); ++i)
            {
                QString tmp = selList.at(i)->text();
                params.append(tmp.mid(2));
            }
        }
        else
        {
            //Add all untracked files
            params << "add";
        }

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_ADD;
    }
}

int MainWindow::getCommentOrTag(QString& commentOrTag, QString question, QString dlgTitle)
{
    int ret;

    QDialog dlg(this);

    QLabel *commentLabel = new QLabel(question);
    QLineEdit *commentOrTagEdit = new QLineEdit;
    commentOrTagEdit -> setFixedWidth(400);
    QHBoxLayout *commentLayout = new QHBoxLayout;
    commentLayout -> addWidget(commentLabel);
    commentLayout -> addWidget(commentOrTagEdit);

    QPushButton *btnOk = new QPushButton(tr("Ok"));
    QPushButton *btnCancel = new QPushButton(tr("Cancel"));
    QHBoxLayout *btnLayout = new QHBoxLayout;
    btnLayout -> addWidget(btnOk);
    btnLayout -> addWidget(btnCancel);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout -> addLayout(commentLayout);
    mainLayout -> addLayout(btnLayout);

    dlg.setLayout(mainLayout);

    dlg.setWindowTitle(dlgTitle);

    connect(btnOk, SIGNAL(clicked()), &dlg, SLOT(accept()));
    connect(btnCancel, SIGNAL(clicked()), &dlg, SLOT(reject()));

    ret = dlg.exec();
    commentOrTag = commentOrTagEdit -> text();
    return ret;
}

void MainWindow::hgCommit()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        QString comment;
        
        if (QDialog::Accepted == getCommentOrTag(comment, tr("Comment:"), tr("Save (commit)")))
        {
            if (!comment.isEmpty())
            {
                if ((justMerged == false) && (areAllSelectedCommitable(hgExp -> workFolderFileList)))
                {
                    //User wants to commit selected file(s) (and this is not merge commit, which would fail if we selected files)
                    params << "commit" << "--message" << comment << "--user" << getUserInfo() << "--";

                    QList <QListWidgetItem *> selList = hgExp -> workFolderFileList -> selectedItems();
                    for (int i = 0; i < selList.size(); ++i)
                    {
                        QString tmp = selList.at(i)->text();
                        params.append(tmp.mid(2));
                    }
                }
                else
                {
                    //Commit all changes
                    params << "commit" << "--message" << comment << "--user" << getUserInfo();
                }

                runner -> startHgCommand(workFolderPath, params);
                runningAction = ACT_COMMIT;
            }
        }
    }
}

QString MainWindow::filterTag(QString tag)
{
    for(int i = 0; i < tag.size(); i++)
    {
        if (tag[i].isLower() || tag[i].isUpper() || tag[i].isDigit() || (tag[i] == QChar('.')))
        {
            //ok
        }
        else
        {
            tag[i] = QChar('_');
        }
    }
    return tag;
}


void MainWindow::hgTag()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        QString tag;

        if (QDialog::Accepted == getCommentOrTag(tag, tr("Tag:"), tr("Tag")))
        {
            if (!tag.isEmpty())
            {
                params << "tag" << "--user" << getUserInfo() << filterTag(tag);

                runner -> startHgCommand(workFolderPath, params);
                runningAction = ACT_TAG;
            }
        }
    }
}


void MainWindow::hgIgnore()
{
    if (runningAction == ACT_NONE)
    {
        QString hgIgnorePath;
        QStringList params;
        QString editorName;

        hgIgnorePath = workFolderPath;
        hgIgnorePath += ".hgignore";

        params << hgIgnorePath;

        if ((getSystem() == "Linux"))
        {
            editorName = "gedit";
        }
        else
        {
            editorName = """C:\\Program Files\\Windows NT\\Accessories\\wordpad.exe""";
        }

        runner -> startCommand(editorName, workFolderPath, params);
        runningAction = ACT_HG_IGNORE;
    }
}



void MainWindow::hgFileDiff()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        QString currentFile = hgExp -> getCurrentFileListLine();

        if (!currentFile.isEmpty())
        {
            //Diff parent file against working folder file
            params << "kdiff3" << "--" << currentFile.mid(2);
            runner -> startHgCommand(workFolderPath, params);
            runningAction = ACT_FILEDIFF;
        }
    }
}


void MainWindow::hgFolderDiff()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        //Diff parent against working folder (folder diff)
        params << "kdiff3";
        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_FOLDERDIFF;
    }
}


void MainWindow::hgChgSetDiff()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        //Diff 2 history log versions against each other
        QString revA;
        QString revB;

        hgExp -> getHistoryDiffRevisions(revA, revB);

        if ((!revA.isEmpty()) && (!revB.isEmpty()))
        {
            params << "kdiff3" << "--rev" << revA << "--rev" << revB;
            runner -> startHgCommand(workFolderPath, params);
            runningAction = ACT_CHGSETDIFF;
        }
        else
        {
            QMessageBox::information(this, tr("Changeset diff"), tr("Please select two changesets from history list or heads list first."));
        }
    }
}



void MainWindow::hgUpdate()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;


        params << "update";


        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_UPDATE;
    }
}


void MainWindow::hgUpdateToRev()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        QString rev;

        hgExp -> getUpdateToRevRevision(rev);

        hgExp -> setCurrentIndex(WORKTAB);
        enableDisableActions();

        params << "update" << "--rev" << rev << "--clean";

        runner -> startHgCommand(workFolderPath, params);

        runningAction = ACT_UPDATE;
    }
}


void MainWindow::hgRevert()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        QString currentFile = hgExp -> getCurrentFileListLine();

        params << "revert" << "--no-backup" << "--" << currentFile.mid(2);

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_REVERT;
    }
}

void MainWindow::hgRetryMerge()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        params << "resolve" << "--all";
        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_RETRY_MERGE;
    }
}


void MainWindow::hgMerge()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        params << "merge";

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_MERGE;
    }
}


void MainWindow::hgCloneFromRemote()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        params << "clone" << remoteRepoPath << workFolderPath;

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_CLONEFROMREMOTE;
    }
}


void MainWindow::hgInit()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        params << "init";

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_INIT;
    }
}


void MainWindow::hgIncoming()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        params << "incoming" << "--newest-first" << remoteRepoPath;

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_INCOMING;
    }
}


void MainWindow::hgPull()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        params << "pull" << remoteRepoPath;

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_PULL;
    }
}


void MainWindow::hgPush()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;

        params << "push" << remoteRepoPath;

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_PUSH;
    }
}


QString MainWindow::listAllUpIpV4Addresses()
{
    QString ret;
    QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();

    for (int i = 0; i < ifaces.count(); i++)
    {
        QNetworkInterface iface = ifaces.at(i);

        if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack))
        {
            for (int j=0; j<iface.addressEntries().count(); j++)
            {
                QHostAddress tmp = iface.addressEntries().at(j).ip();
                if (QAbstractSocket::IPv4Protocol == tmp.protocol())
                {
                    if (!ret.isEmpty())
                    {
                        ret += " ";
                    }
                    ret += tmp.toString();
                }
            }
        }
    }
    return ret;
}


void MainWindow::hgServe()
{
    if (runningAction == ACT_NONE)
    {
        QStringList params;
        QString msg;

        QString addrs = listAllUpIpV4Addresses();
        QTextStream(&msg) << "Server running on address(es) (" << addrs << "), port 8000";
        params << "serve";

        runner -> startHgCommand(workFolderPath, params);
        runningAction = ACT_SERVE;

        QMessageBox::information(this, "Serve", msg, QMessageBox::Close);
        runner -> killCurrentCommand();
    }
}


void MainWindow::startupDialog()
{
    StartupDialog *dlg = new StartupDialog(this);
    if (dlg->exec()) firstStart = false;
}
    

void MainWindow::open()
{
    MultiChoiceDialog *d = new MultiChoiceDialog
        (tr("Open Repository"),
         tr("<qt><big>What would you like to open?</big></qt>"),
         this);

    d->addChoice("remote",
                 tr("<qt><center><img src=\":images/browser-64.png\"><br>Remote repository</center></qt>"),
                 tr("Open a remote Mercurial repository, by cloning from its URL into a local folder."),
                 MultiChoiceDialog::UrlToDirectoryArg);

    d->addChoice("local",
                 tr("<qt><center><img src=\":images/hglogo-64.png\"><br>Local repository</center></qt>"),
                 tr("Open an existing local Mercurial repository."),
                 MultiChoiceDialog::DirectoryArg);

    d->addChoice("init",
                 tr("<qt><center><img src=\":images/hdd_unmount-64.png\"><br>File folder</center></qt>"),
                 tr("Open a local folder, by creating a Mercurial repository in it."),
                 MultiChoiceDialog::DirectoryArg);
    
    d->setCurrentChoice("local");

    if (d->exec() == QDialog::Accepted) {

        QString choice = d->getCurrentChoice();
        QString arg = d->getArgument().trimmed();
    
        if (choice == "local") {
            DEBUG << "open " << arg << endl;
            workFolderPath = arg;
            remoteRepoPath = "";
        } else if (choice == "remote") {
            DEBUG << "clone " << arg << " to " << d->getAdditionalArgument().trimmed() << endl;
            //!!! check that work folder does not exist, append to it if it does
        } else if (choice == "init") {
            DEBUG << "init " << arg << endl;
            //!!!
        }
        
        hgExp->clearLists();
        enableDisableActions();
        hgPaths();
    }

    delete d;
}

void MainWindow::settings()
{
/*!!!
    SettingsDialog *settingsDlg = new SettingsDialog(this);
    settingsDlg->setModal(true);
    settingsDlg->exec();
    hgExp -> clearLists();
    enableDisableActions();
    hgStat();
*/
}

#define STDOUT_NEEDS_BIG_WINDOW 512
#define SMALL_WND_W     500
#define SMALL_WND_H     300

#define BIG_WND_W       1024
#define BIG_WND_H       768


void MainWindow::presentLongStdoutToUser(QString stdo)
{
    if (!stdo.isEmpty())
    {
        QDialog dlg;

        if (stdo.length() > STDOUT_NEEDS_BIG_WINDOW)
        {
            dlg.setMinimumWidth(BIG_WND_W);
            dlg.setMinimumHeight(BIG_WND_H);
        }
        else
        {
            dlg.setMinimumWidth(SMALL_WND_W);
            dlg.setMinimumHeight(SMALL_WND_H);
        }

        QVBoxLayout *box = new QVBoxLayout;
        QListWidget *list = new QListWidget;
        list-> addItems(stdo.split("\n"));
        QPushButton *btn = new QPushButton(tr("Ok"));
        connect(btn, SIGNAL(clicked()), &dlg, SLOT(accept()));

        box -> addWidget(list);
        box -> addWidget(btn);
        dlg.setLayout(box);

        dlg.exec();
    }
    else
    {
        QMessageBox::information(this, tr("HgExplorer"), tr("Mercurial command did not return any output."));
    }
}


bool MainWindow::areAllSelectedCommitable(QListWidget *workList)
{
    QList<QListWidgetItem *> selList = workList -> selectedItems();
    for (int i = 0; i < selList.size(); ++i)
    {
        QString tmp = selList.at(i) -> text().mid(0, 1);
        if (tmp == "A")
        {
            //scheduled to be added, ok to commit
        }
        else if (tmp == "M")
        {
            //locally modified, ok to commit
        }
        else if (tmp == "R")
        {
            //user wants to remove from repo, ok to commit
        }
        else
        {
            return false;
        }
    }
    return true;
}

bool MainWindow::isSelectedDeletable(QListWidget *workList)
{
    QList<QListWidgetItem *> selList = workList -> selectedItems();
    if (selList.count() == 1)
    {
        QString tmp = selList.at(0)->text().mid(0, 1);
        if (tmp == "A")
        {
            //scheduled to be added, ok to remove (won't go to repo)
            return true;
        }
        else if (tmp == "C")
        {
            //Tracked but unchanged, ok to remove
            return true;
        }
        else if (tmp == "M")
        {
            //locally modified, ok to remove from repo
            return true;
        }
        else if (tmp == "!")
        {
            //locally deleted, ok to remove from repo
            return true;
        }
    }
    return false;
}


bool MainWindow::areAllSelectedUntracked(QListWidget *workList)
{
    QList<QListWidgetItem *> selList = workList -> selectedItems();
    for (int i = 0; i < selList.size(); ++i)
    {
        QString tmp = selList.at(i) -> text();

        if (tmp.mid(0,1) != "?")
        {
            return false;
        }
    }
    return true;
}


bool MainWindow::isSelectedModified(QListWidget *workList)
{
    QList<QListWidgetItem *> selList = workList -> selectedItems();
    if (selList.count() == 1)
    {
        if (selList.at(0)->text().mid(0, 1) == "M")
        {
            return true;
        }
    }
    return false;
}

void MainWindow::countModifications(QListWidget *workList, int& added, int& modified, int& removed, int& notTracked,
                                    int& selected,
                                    int& selectedAdded, int& selectedModified, int& selectedRemoved, int& selectedNotTracked)
{
    int itemCount = workList -> count();
    if (itemCount > 0)
    {
        int A= 0;
        int M=0;
        int R=0;
        int N=0;
        int S=0;
        int SA=0;
        int SM=0;
        int SR=0;
        int SN=0;

        for (int i = 0; i < workList -> count(); i++)
        {
            QListWidgetItem *currItem = workList -> item(i);

            QString tmp = currItem->text().mid(0, 1);
            if (tmp == "M")
            {
                M++;
            }
            else if (tmp == "R")
            {
                R++;
            }
            else if (tmp == "A")
            {
                A++;
            }
            else if (tmp == "?")
            {
                N++;
            }
        }

        added = A;
        modified = M;
        removed = R;
        notTracked = N;

        QList <QListWidgetItem *> selList = workList -> selectedItems();

        S = selList.size();
        for (int i = 0; i < selList.size(); ++i)
        {
            QString tmp = selList.at(i) -> text();

            if (tmp.mid(0,1) == "A")
            {
                SA++;
            }
            else if (tmp.mid(0,1) == "M")
            {
                SM++;
            }
            else if (tmp.mid(0,1) == "R")
            {
                SR++;
            }
            else if (tmp.mid(0,1) == "?")
            {
                SN++;
            }
        }

        selected = S;
        selectedAdded = SA;
        selectedModified = SM;
        selectedRemoved = SR;
        selectedNotTracked = SN;
    }
    else
    {
        added = 0;
        modified = 0;
        removed = 0;
        notTracked = 0;
        selected = 0;
        selectedAdded = 0;
        selectedModified = 0;
        selectedRemoved = 0;
        selectedNotTracked = 0;
    }
}


void MainWindow::commandFailed()
{
    DEBUG << "MainWindow::commandFailed" << endl;
    runningAction = ACT_NONE;
    runner -> hideProgBar();

    //!!! N.B hg incoming returns failure even if successful, if there were no changes
}

void MainWindow::commandCompleted()
{
    bool shouldHgStat = false;

    if (runningAction != ACT_NONE)
    {
        //We are running some hg command...
        if (runner -> isCommandRunning() == false)
        {
            //Running has just ended.
            int exitCode = runner -> getExitCode();

            runner -> hideProgBar();

            //Clumsy...
            if ((EXITOK(exitCode)) || ((exitCode == 1) && (runningAction == ACT_INCOMING)))
            {
                //Successful running.
                switch(runningAction)
                {
                    case ACT_PATHS:
                    {
                        QString sout = runner->getStdOut();
                        DEBUG << "stdout is " << sout << endl;
                        LogParser lp(sout, "=");
                        LogList ll = lp.parse();
                        DEBUG << ll.size() << " results" << endl;
                        if (!ll.empty()) {
                            remoteRepoPath = lp.parse()[0]["default"].trimmed();
                            DEBUG << "Set remote path to " << remoteRepoPath << endl;
                        }
                        MultiChoiceDialog::addRecentArgument("local", workFolderPath);
                        MultiChoiceDialog::addRecentArgument("remote", remoteRepoPath);
                        enableDisableActions();
                        break;
                    }

                    case ACT_STAT:
                        hgExp -> updateWorkFolderFileList(runner -> getStdOut());
                        break;

                    case ACT_INCOMING:
                    case ACT_ANNOTATE:
                    case ACT_RESOLVE_LIST:
                    case ACT_RESOLVE_MARK:
                        presentLongStdoutToUser(runner -> getStdOut());
                        shouldHgStat = true;
                        break;

                    case ACT_PULL:
                        QMessageBox::information(this, "Pull", runner -> getStdOut());
                        shouldHgStat = true;
                        break;

                    case ACT_PUSH:
                        QMessageBox::information(this, "Push", runner -> getStdOut());
                        shouldHgStat = true;
                        break;

                    case ACT_INIT:
                        MultiChoiceDialog::addRecentArgument("init", workFolderPath);
                        MultiChoiceDialog::addRecentArgument("local", workFolderPath);
                        enableDisableActions();
                        shouldHgStat = true;
                        break;

                    case ACT_CLONEFROMREMOTE:
                        MultiChoiceDialog::addRecentArgument("local", workFolderPath);
                        MultiChoiceDialog::addRecentArgument("remote", remoteRepoPath);
                        MultiChoiceDialog::addRecentArgument("remote", workFolderPath, true);
                        QMessageBox::information(this, "Clone", runner -> getStdOut());
                        enableDisableActions();
                        shouldHgStat = true;
                        break;

                    case ACT_LOG:
                        {
                            hgExp -> updateLocalRepoHgLogList(runner -> getStdOut());
                        }
                        break;

                    case ACT_PARENTS:
                        {
                            hgExp -> updateLocalRepoParentsList(runner -> getStdOut());
                        }
                        break;

                    case ACT_HEADS:
                        {
                            QString stdOut = runner -> getStdOut();
                            hgExp -> updateLocalRepoHeadsList(stdOut);
                        }
                        break;

                    case ACT_REMOVE:
                    case ACT_ADD:
                    case ACT_COMMIT:
                    case ACT_FILEDIFF:
                    case ACT_FOLDERDIFF:
                    case ACT_CHGSETDIFF:
                    case ACT_REVERT:
                    case ACT_SERVE:
                    case ACT_TAG:
                    case ACT_HG_IGNORE:
                        shouldHgStat = true;
                        break;

                    case ACT_UPDATE:
                        QMessageBox::information(this, tr("Update"), runner -> getStdOut());
                        shouldHgStat = true;
                        break;

                    case ACT_MERGE:
                        QMessageBox::information(this, tr("Merge"), runner -> getStdOut());
                        shouldHgStat = true;
                        justMerged = true;
                        break;

                    case ACT_RETRY_MERGE:
                        QMessageBox::information(this, tr("Merge retry"), tr("Merge retry successful."));
                        shouldHgStat = true;
                        justMerged = true;
                        break;

                    default:
                        break;
                }
            }


            //Typical sequence goes paths -> stat -> heads -> parents -> log
            if (runningAction == ACT_PATHS)
            {
                runningAction = ACT_NONE;
                hgStat();
            }
            else if (runningAction == ACT_STAT)
            {
                runningAction = ACT_NONE;
                hgHeads();
            }
            else if (runningAction == ACT_HEADS)
            {
                runningAction = ACT_NONE;
                hgParents();
            }
            else if (runningAction == ACT_PARENTS)
            {
                runningAction = ACT_NONE;
                hgLog();
            }
            else if ((runningAction == ACT_MERGE) && (exitCode != 0))
            {
                //If we had a failed merge, offer to retry
                if (QMessageBox::Ok == QMessageBox::information(this, tr("Retry merge ?"), tr("Merge attempt failed. retry ?"), QMessageBox::Ok | QMessageBox::Cancel))
                {
                    runningAction = ACT_NONE;
                    hgRetryMerge();
                }
                else
                {
                    runningAction = ACT_NONE;
                    hgStat();
                }
            }
            else
            {
                runningAction = ACT_NONE;
                if (shouldHgStat)
                {
                    hgPaths();
                }
            }
        }
    }
    else
    {
        enableDisableActions();
    }
}

void MainWindow::connectActions()
{
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));

    connect(hgStatAct, SIGNAL(triggered()), this, SLOT(hgPaths()));
    connect(hgExp, SIGNAL(workFolderViewTypesChanged()), this, SLOT(hgPaths()));
    connect(hgRemoveAct, SIGNAL(triggered()), this, SLOT(hgRemove()));
    connect(hgAddAct, SIGNAL(triggered()), this, SLOT(hgAdd()));
    connect(hgCommitAct, SIGNAL(triggered()), this, SLOT(hgCommit()));
    connect(hgFileDiffAct, SIGNAL(triggered()), this, SLOT(hgFileDiff()));
    connect(hgFolderDiffAct, SIGNAL(triggered()), this, SLOT(hgFolderDiff()));
    connect(hgChgSetDiffAct, SIGNAL(triggered()), this, SLOT(hgChgSetDiff()));
    connect(hgUpdateAct, SIGNAL(triggered()), this, SLOT(hgUpdate()));
    connect(hgRevertAct, SIGNAL(triggered()), this, SLOT(hgRevert()));
    connect(hgMergeAct, SIGNAL(triggered()), this, SLOT(hgMerge()));
    connect(hgRetryMergeAct, SIGNAL(triggered()), this, SLOT(hgRetryMerge()));
    connect(hgTagAct, SIGNAL(triggered()), this, SLOT(hgTag()));
    connect(hgIgnoreAct, SIGNAL(triggered()), this, SLOT(hgIgnore()));

    connect(settingsAct, SIGNAL(triggered()), this, SLOT(settings()));
    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

    connect(hgInitAct, SIGNAL(triggered()), this, SLOT(hgInit()));
    connect(hgCloneFromRemoteAct, SIGNAL(triggered()), this, SLOT(hgCloneFromRemote()));
    connect(hgIncomingAct, SIGNAL(triggered()), this, SLOT(hgIncoming()));
    connect(hgPullAct, SIGNAL(triggered()), this, SLOT(hgPull()));
    connect(hgPushAct, SIGNAL(triggered()), this, SLOT(hgPush()));

    connect(hgExp, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));

    connect(hgUpdateToRevAct, SIGNAL(triggered()), this, SLOT(hgUpdateToRev()));
    connect(hgAnnotateAct, SIGNAL(triggered()), this, SLOT(hgAnnotate()));
    connect(hgResolveListAct, SIGNAL(triggered()), this, SLOT(hgResolveList()));
    connect(hgResolveMarkAct, SIGNAL(triggered()), this, SLOT(hgResolveMark()));
    connect(hgServeAct, SIGNAL(triggered()), this, SLOT(hgServe()));
}

void MainWindow::tabChanged(int currTab)
{
    tabPage = currTab;

}

void MainWindow::enableDisableActions()
{
    QDir localRepoDir;
    QDir workFolderDir;
    bool workFolderExist;
    bool localRepoExist;

    remoteRepoActionsEnabled = true;
    if (remoteRepoPath.isEmpty())
    {
        remoteRepoActionsEnabled = false;
    }

    localRepoActionsEnabled = true;
    if (workFolderPath.isEmpty())
    {
        localRepoActionsEnabled = false;
        workFolderExist = false;
    }

    if (!workFolderDir.exists(workFolderPath))
    {
        localRepoActionsEnabled = false;
        workFolderExist = false;
    }
    else
    {
        workFolderExist = true;
    }

    if (!localRepoDir.exists(workFolderPath + getHgDirName()))
    {
        localRepoActionsEnabled = false;
        localRepoExist = false;
    }

    hgCloneFromRemoteAct -> setEnabled(remoteRepoActionsEnabled);
    hgIncomingAct -> setEnabled(remoteRepoActionsEnabled && remoteRepoActionsEnabled);
    hgPullAct -> setEnabled(remoteRepoActionsEnabled && remoteRepoActionsEnabled);
    hgPushAct -> setEnabled(remoteRepoActionsEnabled && remoteRepoActionsEnabled);
/*
    if (tabPage != WORKTAB)
    {
        localRepoActionsEnabled = false;
    }
*/
    hgInitAct -> setEnabled((localRepoExist == false) && (workFolderExist==true));
    hgStatAct -> setEnabled(localRepoActionsEnabled);
    hgFileDiffAct -> setEnabled(localRepoActionsEnabled);
    hgFolderDiffAct -> setEnabled(localRepoActionsEnabled);
    hgRevertAct -> setEnabled(localRepoActionsEnabled);
    hgAddAct -> setEnabled(localRepoActionsEnabled);
    hgRemoveAct -> setEnabled(localRepoActionsEnabled);
    hgUpdateAct -> setEnabled(localRepoActionsEnabled);
    hgCommitAct -> setEnabled(localRepoActionsEnabled);
    hgMergeAct -> setEnabled(localRepoActionsEnabled);
    hgRetryMergeAct -> setEnabled(localRepoActionsEnabled);
    hgResolveListAct -> setEnabled(localRepoActionsEnabled);
    hgResolveMarkAct -> setEnabled(localRepoActionsEnabled);
    hgAnnotateAct -> setEnabled(localRepoActionsEnabled);
    hgServeAct -> setEnabled(localRepoActionsEnabled);
    hgTagAct -> setEnabled(localRepoActionsEnabled);
    hgIgnoreAct -> setEnabled(localRepoActionsEnabled);

    hgExp -> enableDisableOtherTabs(tabPage);

    int added, modified, removed, notTracked, selected, selectedAdded, selectedModified, selectedRemoved, selectedNotTracked;

    countModifications(hgExp -> workFolderFileList,
        added, modified, removed, notTracked,
        selected,
        selectedAdded, selectedModified, selectedRemoved, selectedNotTracked);

    if (tabPage == WORKTAB)
    {
        //Enable / disable actions according to workFolderFileList selections / currentSelection / count
        hgChgSetDiffAct -> setEnabled(false);
        hgUpdateToRevAct -> setEnabled(false);

        if (localRepoActionsEnabled)
        {
            if ((added == 0) && (modified == 0) && (removed == 0))
            {
                hgCommitAct -> setEnabled(false);
                hgRevertAct -> setEnabled(false);
            }
            else if (selected != 0)
            {
                if (selectedNotTracked != 0)
                {
                    hgCommitAct -> setEnabled(false);
                }
                else if ((selectedAdded == 0) && (selectedModified == 0) && (selectedRemoved == 0))
                {
                    hgCommitAct -> setEnabled(false);
                }
            }

            if (modified == 0)
            {
                hgFolderDiffAct -> setEnabled(false);
            }

            if (!isSelectedModified(hgExp -> workFolderFileList))
            {
                hgFileDiffAct -> setEnabled(false);
                hgRevertAct -> setEnabled(false);
            }

            //JK 14.5.2010: Fixed confusing add button. Now this is simple: If we have something to add (any non-tracked files), add is enabled.
            if (notTracked == 0)
            {
                hgAddAct -> setEnabled(false);
            }

            if (!isSelectedDeletable(hgExp -> workFolderFileList))
            {
                hgRemoveAct -> setEnabled(false);
            }

            hgResolveListAct -> setEnabled(true);

            if (hgExp -> localRepoHeadsList->count() < 2)
            {
                hgMergeAct -> setEnabled(false);
                hgRetryMergeAct -> setEnabled(false);
            }

            if (hgExp -> localRepoHeadsList->count() < 1)
            {
                hgTagAct -> setEnabled(false);
            }

            QString currentFile = hgExp -> getCurrentFileListLine();
            if (!currentFile.isEmpty())
            {
                hgAnnotateAct -> setEnabled(true);
                hgResolveMarkAct -> setEnabled(true);
            }
            else
            {
                hgAnnotateAct -> setEnabled(false);
                hgResolveMarkAct -> setEnabled(false);
            }
        }
    }
    else
    {
        QList <QListWidgetItem *> headSelList = hgExp -> localRepoHeadsList->selectedItems();
        QList <QListWidgetItem *> historySelList = hgExp -> localRepoHgLogList->selectedItems();

        if ((historySelList.count() == 2) || (headSelList.count() == 2))
        {
            hgChgSetDiffAct -> setEnabled(true);
        }
        else
        {
            hgChgSetDiffAct -> setEnabled(false);
        }

        if (historySelList.count() == 1)
        {
            hgUpdateToRevAct -> setEnabled(true);
        }
        else
        {
            hgUpdateToRevAct -> setEnabled(false);
        }
    }
}

void MainWindow::createActions()
{
    //File actions
    hgInitAct = new QAction(tr("Init local repository"), this);
    hgInitAct->setStatusTip(tr("Create an empty local repository in selected folder"));

    hgCloneFromRemoteAct = new QAction(tr("Clone from remote"), this);
    hgCloneFromRemoteAct->setStatusTip(tr("Clone from remote repository into local repository in selected folder"));

    openAct = new QAction(QIcon(":/images/fileopen.png"), tr("Open..."), this);
    openAct -> setStatusTip(tr("Open repository"));
    openAct -> setIconVisibleInMenu(true);

    settingsAct = new QAction(QIcon(":/images/settings.png"), tr("Settings..."), this);
    settingsAct -> setStatusTip(tr("View and change application settings"));
    settingsAct -> setIconVisibleInMenu(true);

    exitAct = new QAction(QIcon(":/images/exit.png"), tr("Exit"), this);
    exitAct->setShortcuts(QKeySequence::Quit);
    exitAct->setStatusTip(tr("Exit application"));
    exitAct -> setIconVisibleInMenu(true);

    //Repository actions
    hgStatAct = new QAction(QIcon(":/images/status.png"), tr("Refresh"), this);
    hgStatAct->setStatusTip(tr("Refresh (info of) status of workfolder files"));

    hgIncomingAct = new QAction(QIcon(":/images/incoming.png"), tr("Preview"), this);
    hgIncomingAct -> setStatusTip(tr("View info of changesets incoming to us from remote repository (on pull operation)"));

    hgPullAct = new QAction(QIcon(":/images/pull.png"), tr("Pull"), this);
    hgPullAct -> setStatusTip(tr("Pull changesets from remote repository to local repository"));

    hgPushAct = new QAction(QIcon(":/images/push.png"), tr("Push"), this);
    hgPushAct->setStatusTip(tr("Push local changesets to remote repository"));

    //Workfolder actions
    hgFileDiffAct   = new QAction(QIcon(":/images/diff.png"), tr("Diff"), this);
    hgFileDiffAct->setStatusTip(tr("Filediff: View differences between selected working folder file and local repository file"));

    hgFolderDiffAct   = new QAction(QIcon(":/images/folderdiff.png"), tr("View folderdiff"), this);
    hgFolderDiffAct->setStatusTip(tr("Folderdiff: View all differences between working folder files and local repository files"));

    hgChgSetDiffAct   = new QAction(QIcon(":/images/chgsetdiff.png"), tr("View changesetdiff"), this);
    hgChgSetDiffAct->setStatusTip(tr("Change set diff: View differences between all files of 2 repository changesets"));

    hgRevertAct = new QAction(QIcon(":/images/undo.png"), tr("Revert"), this);
    hgRevertAct->setStatusTip(tr("Undo selected working folder file changes (return to local repository version)"));

    hgAddAct = new QAction(QIcon(":/images/add.png"), tr("Add"), this);
    hgAddAct -> setStatusTip(tr("Add working folder file(s) (selected or all yet untracked) to local repository (on next commit)"));

    hgRemoveAct = new QAction(QIcon(":/images/remove.png"), tr("Remove"), this);
    hgRemoveAct -> setStatusTip(tr("Remove selected working folder file from local repository (on next commit)"));

    hgUpdateAct = new QAction(QIcon(":/images/update.png"), tr("Update"), this);
    hgUpdateAct->setStatusTip(tr("Update working folder from local repository"));

    hgCommitAct = new QAction(QIcon(":/images/commit.png"), tr("Commit"), this);
    hgCommitAct->setStatusTip(tr("Save selected file(s) or all changed files in working folder (and all subfolders) to local repository"));

    hgMergeAct = new QAction(QIcon(":/images/merge.png"), tr("Merge"), this);
    hgMergeAct->setStatusTip(tr("Merge two local repository changesets to working folder"));

    //Advanced actions
    hgUpdateToRevAct = new QAction(tr("Update to revision"), this);
    hgUpdateToRevAct -> setStatusTip(tr("Update working folder to version selected from history list"));

    hgAnnotateAct = new QAction(tr("Annotate"), this);
    hgAnnotateAct -> setStatusTip(tr("Show line-by-line version information for selected file"));

    hgResolveListAct = new QAction(tr("Resolve (list)"), this);
    hgResolveListAct -> setStatusTip(tr("Resolve (list): Show list of files needing merge"));

    hgResolveMarkAct = new QAction(tr("Resolve (mark)"), this);
    hgResolveMarkAct -> setStatusTip(tr("Resolve (mark): Mark selected file status as resolved"));

    hgRetryMergeAct = new QAction(tr("Retry merge"), this);
    hgRetryMergeAct -> setStatusTip(tr("Retry merge after failed merge attempt."));

    hgTagAct = new QAction(tr("Tag revision"), this);
    hgTagAct -> setStatusTip(tr("Give decsriptive name (tag) to current workfolder parent revision."));

    hgIgnoreAct = new QAction(tr("Edit .hgignore"), this);
    hgIgnoreAct -> setStatusTip(tr("Edit .hgignore file (file contains names of files that should be ignored by mercurial)"));

    hgServeAct = new QAction(tr("Serve (via http)"), this);
    hgServeAct -> setStatusTip(tr("Serve local repository via http for workgroup access"));

    //Help actions
    aboutAct = new QAction(tr("About"), this);
    aboutAct->setStatusTip(tr("Show the application's About box"));

    aboutQtAct = new QAction(tr("About Qt"), this);
    aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
}

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("File"));
    fileMenu -> addAction(hgInitAct);
    fileMenu -> addAction(hgCloneFromRemoteAct);
    fileMenu -> addSeparator();
    fileMenu -> addAction(openAct);
    fileMenu -> addAction(settingsAct);
    fileMenu -> addSeparator();
    fileMenu -> addAction(exitAct);

    advancedMenu = menuBar()->addMenu(tr("Advanced"));
    advancedMenu -> addAction(hgUpdateToRevAct);
    advancedMenu -> addSeparator();
    advancedMenu -> addAction(hgAnnotateAct);
    advancedMenu -> addSeparator();
    advancedMenu -> addAction(hgRetryMergeAct);
    advancedMenu -> addAction(hgResolveListAct);
    advancedMenu -> addAction(hgResolveMarkAct);
    advancedMenu -> addSeparator();
    advancedMenu -> addAction(hgTagAct);
    advancedMenu -> addSeparator();
    advancedMenu -> addAction(hgIgnoreAct);
    advancedMenu -> addSeparator();
    advancedMenu -> addAction(hgServeAct);

    helpMenu = menuBar()->addMenu(tr("Help"));
    helpMenu->addAction(aboutAct);
    helpMenu->addAction(aboutQtAct);
}

void MainWindow::createToolBars()
{
    fileToolBar = addToolBar(tr("File"));
    fileToolBar -> setIconSize(QSize(MY_ICON_SIZE, MY_ICON_SIZE));
    fileToolBar -> addAction(openAct);
    fileToolBar -> addAction(hgStatAct);
    fileToolBar -> addSeparator();
//    fileToolBar -> addAction(hgChgSetDiffAct);
    fileToolBar -> setMovable(false);

    repoToolBar = addToolBar(tr(REPOMENU_TITLE));
    repoToolBar -> setIconSize(QSize(MY_ICON_SIZE, MY_ICON_SIZE));
    repoToolBar->addAction(hgIncomingAct);
    repoToolBar->addAction(hgPullAct);
    repoToolBar->addAction(hgPushAct);
    repoToolBar -> setMovable(false);

    workFolderToolBar = addToolBar(tr(WORKFOLDERMENU_TITLE));
    addToolBar(Qt::LeftToolBarArea, workFolderToolBar);
    workFolderToolBar -> setIconSize(QSize(MY_ICON_SIZE, MY_ICON_SIZE));
//    workFolderToolBar->addSeparator();
    workFolderToolBar->addAction(hgFileDiffAct);
//    workFolderToolBar->addAction(hgFolderDiffAct);
    workFolderToolBar->addSeparator();
    workFolderToolBar->addAction(hgRevertAct);
    workFolderToolBar->addAction(hgUpdateAct);
    workFolderToolBar->addAction(hgCommitAct);
    workFolderToolBar->addAction(hgMergeAct);
    workFolderToolBar->addSeparator();
    workFolderToolBar->addAction(hgAddAct);
    workFolderToolBar->addAction(hgRemoveAct);
    workFolderToolBar -> setMovable(false);

    foreach (QToolButton *tb, findChildren<QToolButton *>()) {
        tb->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    }
}


void MainWindow::createStatusBar()
{
    statusBar()->showMessage(tr("Ready"));
}


//!!! review these:

void MainWindow::readSettings()
{
    QDir workFolder;

    QSettings settings;

    remoteRepoPath = settings.value("remoterepopath", "").toString();
    workFolderPath = settings.value("workfolderpath", "").toString();
    if (!workFolder.exists(workFolderPath))
    {
        workFolderPath = "";
    }

    QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
    QSize size = settings.value("size", QSize(400, 400)).toSize();
    firstStart = settings.value("firststart", QVariant(true)).toBool();

    initialFileTypesBits = (unsigned char) settings.value("viewFileTypes", QVariant(DEFAULT_HG_STAT_BITS)).toInt();
    resize(size);
    move(pos);
}


void MainWindow::writeSettings()
{
    QSettings settings;
    settings.setValue("pos", pos());
    settings.setValue("size", size());
    settings.setValue("remoterepopath", remoteRepoPath);
    settings.setValue("workfolderpath", workFolderPath);
    settings.setValue("firststart", firstStart);
    settings.setValue("viewFileTypes", hgExp -> getFileTypesBits());
}