Update GPL blurb in source files.
[toast/confclerk.git] / src / gui / searchtabcontainer.cpp
index b4c0be4..f14b19c 100644 (file)
@@ -1,13 +1,31 @@
+/*
+ * Copyright (C) 2010 Ixonos Plc.
+ *
+ * This file is part of ConfClerk.
+ *
+ * ConfClerk 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.
+ *
+ * ConfClerk is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
+ */
 
 #include "searchtabcontainer.h"
 #include "searchhead.h"
-
+#include <QMessageBox>
 
 SearchTabContainer::SearchTabContainer(QWidget *aParent) : TabContainer( aParent )
 {
     header = new SearchHead(this);
     header->setObjectName(QString::fromUtf8("header"));
-    QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+    QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
     sizePolicy.setHorizontalStretch(0);
     sizePolicy.setVerticalStretch(0);
     //sizePolicy.setHeightForWidth(TabContainer::sizePolicy().hasHeightForWidth());
@@ -32,7 +50,13 @@ SearchTabContainer::SearchTabContainer(QWidget *aParent) : TabContainer( aParent
 
     verticalLayout_2->insertWidget(0,searchAgainButton);
 
-    searchAgainButton->show();
+    searchAgainButton->hide();
+    treeView->hide();
+    // do not show 'search' header if there are no conferences in the DB
+    if(Conference::getAll().count()==0)
+    {
+        header->hide();
+    }
 
     connect( header, SIGNAL(searchClicked()), SLOT(searchButtonClicked()));
     connect( searchAgainButton, SIGNAL(clicked()), SLOT(searchAgainClicked()));
@@ -44,8 +68,6 @@ SearchTabContainer::~SearchTabContainer()
 
 void SearchTabContainer::searchButtonClicked()
 {
-    qDebug() << "SearchTab::searchButtonClicked()";
-
     QHash<QString,QString> columns;
 
     SearchHead *searchHeader = static_cast<SearchHead*>(header);
@@ -61,20 +83,62 @@ void SearchTabContainer::searchButtonClicked()
         columns["ROOM"] = "name";
 
     QString keyword = searchHeader->searchEdit->text().replace( QString("%"), QString("\\%") );
-    qDebug() << "\nKeyword to search: " << keyword;
+    //qDebug() << "\nKeyword to search: " << keyword;
 
     int confId = Conference::activeConference();
     SqlEngine::searchEvent( confId, columns, keyword );
 
     QDate startDate = Conference::getById(confId).start();
     QDate endDate = Conference::getById(confId).end();
-    dayNavigator->setDates(startDate, endDate);
-    updateTreeView( Conference::getById(confId).start() );
+
+    int nrofFounds = 0;
+    QDate firstDateWithFounds = endDate;
+    QDate lastDateWithFounds = startDate;
+    for(QDate d=startDate; d<=endDate; d=d.addDays(1))
+    {
+        try{
+            int count = Event::getSearchResultByDate(d, confId, "start").count();
+            if(count && (firstDateWithFounds==endDate))
+                firstDateWithFounds=d;
+            if(count)
+                lastDateWithFounds=d;
+            nrofFounds+=count;
+        }
+        catch( OrmException &e  ){
+            qDebug() << "Event::getSearchResultByDate failed: " << e.text();
+        }
+        catch(...){
+            qDebug() << "Event::getSearchResultByDate failed";
+        }
+    }
+
+    if(!nrofFounds)
+    {
+        // TODO: display some message
+        treeView->hide();
+        searchAgainButton->hide();
+        dayNavigator->hide();
+        header->show();
+        QMessageBox::information(
+                this,
+                QString("Keyword '%1' not found!").arg(keyword),
+                QString("No events containing '%1' found!").arg(keyword),
+                QMessageBox::Ok);
+    }
+    else
+    {
+        searchAgainButton->show();
+        dayNavigator->show();
+        treeView->show();
+        header->hide();
+
+        updateTreeView( firstDateWithFounds );
+        dayNavigator->setDates(firstDateWithFounds, lastDateWithFounds);
+    }
 }
 
 void SearchTabContainer::searchAgainClicked()
 {
-    qDebug() << "SearchTab::searchAgainClicked()";
     header->show();
     searchAgainButton->hide();
     dayNavigator->hide();
@@ -83,20 +147,6 @@ void SearchTabContainer::searchAgainClicked()
 
 void SearchTabContainer::loadEvents( const QDate &aDate, const int aConferenceId )
 {
-    int eventsCount = static_cast<EventModel*>(treeView->model())->loadSearchResultEvents( aDate, aConferenceId );
-    if( eventsCount ||
-            //TODO: this is not good test
-            dayNavigator->getCurrentDate() != Conference::getById( aConferenceId ).start()
-            ){
-        searchAgainButton->show();
-        dayNavigator->show();
-        treeView->show();
-        header->hide();
-    }
-    else{
-        treeView->hide();
-        searchAgainButton->hide();
-        dayNavigator->hide();
-        header->show();
-    }
+    static_cast<EventModel*>(treeView->model())->loadSearchResultEvents( aDate, aConferenceId );
 }
+