/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          cdblistview.cpp  -  description
                            -------------------
    begin                : Fri Mar 31 2000
    copyright            : (C) 2000-2006 by Alexey S.Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

/***************************************************************************
   This library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Library General Public License as published by
   the Free Software Foundation; either version 2 of the License, or (at 
   your option) any later version.

   This library 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 Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#include <sptk3/CDBListView.h>
#include <sptk3/CFieldList.h>
#include <sptk3/CException.h>
#include <sptk3/CMessageDialog.h>

#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <stdio.h>

#include <sptk3/DebugMacros.h>

using namespace std;
using namespace sptk;

CDBListView::CDBListView(const char *label,int layoutSize,CLayoutAlign layoutAlignment)
: CListView(label,layoutSize,layoutAlignment) {
   m_fastRefreshEnabed = false;
   m_maxRecords = 0;
   m_recordsLimited = false;
}

#ifdef __COMPATIBILITY_MODE__    
CDBListView::CDBListView(int x, int y, int w, int h, const char *label)
: CListView(x, y, w, h, label) {
   m_fastRefreshEnabed = false;
   m_maxRecords = 0;
   m_recordsLimited = false;
}
#endif

CDBListView::~CDBListView() {
   m_fastRefreshQuery.close();
   m_fullRefreshQuery.close();
}

void CDBListView::database(CDatabase *db) {
   if (m_dataMode == LV_DATA_UNDEFINED)
      m_dataMode = LV_DATA_KEY;
   m_fastRefreshQuery.connect(db);
   m_fullRefreshQuery.connect(db);
   m_recordCountQuery.connect(db);
}

CDatabase *CDBListView::database() const {
   return m_fullRefreshQuery.database();
}

void CDBListView::sql(string _sql,string _recordCountSql,string _fastRefreshSQL) {
   m_fullRefreshQuery.sql(_sql);
   m_fastRefreshQuery.sql(_fastRefreshSQL);
   m_recordCountQuery.sql(_recordCountSql);
   m_fastRefreshEnabed = _fastRefreshSQL.length() > 0;
}

string CDBListView::sql() {
   return m_fullRefreshQuery.sql();
}

CParam& CDBListView::param(const char *paramName,CRefreshKind refreshKind) {
   if (m_fastRefreshEnabed && refreshKind == LV_REFRESH_FAST )
      return m_fastRefreshQuery.param(paramName);
   else  return m_fullRefreshQuery.param(paramName);
}

void CDBListView::setup(CDatabase *db,string _sql,string _keyField) {
   database(db);
   sql(_sql);
   keyField(_keyField);
}

void CDBListView::keyField(string keyField) {
   m_keyField = keyField;
}

void CDBListView::refreshData(CRefreshKind refreshKind) {
   m_lastRefresh = CDateTime::Now();

   fl_cursor(FL_CURSOR_WAIT);
   Fl::check();

   unsigned recordsEstimated = 0;
   if (m_recordCountQuery.sql().length()) {
      m_recordCountQuery.open();
      recordsEstimated = m_recordCountQuery[0].asInteger();
      m_recordCountQuery.close();
   }

   CQuery *query = &m_fullRefreshQuery;
   if (m_fastRefreshEnabed && refreshKind == LV_REFRESH_FAST)
      query = &m_fastRefreshQuery;
   else  refreshKind = LV_REFRESH_FULL;

   fill(*query,m_keyField,m_maxRecords,recordsEstimated,refreshKind);
}
