1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2013 Randy Baumgarte
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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************************************/
#include "notemodel.h"
#include "logger/qslog.h"
#include "global.h"
#include "sql/nsqlquery.h"
#include <QString>
#include <QSqlDatabase>
#include <QtSql>
//****************************************************
//* This class is used to read & write information
//* to the NoteModel class. This class holds what
//* a user sees in the table listing in the GUI.
//****************************************************
extern Global global;
// Generic constructor
NoteModel::NoteModel(QObject *parent)
:QSqlTableModel(parent, global.db->conn)
{
// Check if the table exists. If not, create it.
NSqlQuery sql(global.db);
sql.exec("Select * from sqlite_master where type='table' and name='NoteTable';");
if (!sql.next())
this->createTable();
sql.finish();
this->setEditStrategy(QSqlTableModel::OnFieldChange);
this->setTable("NoteTable");
this->setFilter("lid in (select lid from filter)");
}
// Destructor
NoteModel::~NoteModel() {
}
//* Create the NoteModel table.
void NoteModel::createTable() {
QLOG_TRACE() << "Entering NoteModel::createTable()";
QLOG_DEBUG() << "Creating table NoteTable";
NSqlQuery sql(global.db);
QString command("Create table if not exists NoteTable (" +
QString("lid integer primary key,") +
QString("dateCreated real default null,") +
QString("dateUpdated real default null,") +
QString("title text default null collate nocase,") +
QString("notebookLid integer default null,") +
QString("notebook text default null collate nocase,") +
QString("tags text default null collate nocase,") +
QString("author text default null collate nocase,") +
QString("dateSubject real default null,") +
QString("dateDeleted real default null,") +
QString("source text default null collate nocase,") +
QString("sourceUrl text default null collate nocase,") +
QString("sourceApplication text default null collate nocase,") +
QString("latitude real default null,") +
QString("longitude real default null,") +
QString("altitude real default null,") +
QString("hasEncryption integer default null,") +
QString("hasTodo integer default null,") +
QString("isDirty integer default null,") +
QString("size integer default null,") +
QString("reminderOrder real default null,") +
QString("reminderTime real default null,") +
QString("reminderDoneTime real default null,") +
QString("isPinned integer default null,") +
QString("titleColor text default null,") +
QString("thumbnail default null") +
QString(")"));
if (!sql.exec(command) ||
!sql.exec("CREATE INDEX if not exists NoteTable_Title_Index on NoteTable (title)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Author_Index on NoteTable (author)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Notebook_Index on NoteTable (notebook)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Notebook_Lid_Index on NoteTable (notebookLid)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_DateCreated_Index on NoteTable (dateCreated)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_DateUpdated_Index on NoteTable (dateUpdated)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Date_Subject_Index on NoteTable (dateSubject)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Date_Deleted_Index on NoteTable (dateDeleted)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Source_Index on NoteTable (source)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Source_Url_Index on NoteTable (sourceUrl)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Source_Application_Index on NoteTable (sourceApplication)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Latitude_Index on NoteTable (latitude)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Longitude_Index on NoteTable (longitude)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Altitude_Index on NoteTable (altitude)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Has_Encryption_Index on NoteTable (hasEncryption)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Has_Todo_Index on NoteTable (hasTodo)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Is_Dirty_Index on NoteTable (isDirty)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Reminder_Order_Index on NoteTable (reminderOrder)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Reminder_Time_Index on NoteTable (reminderTime)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_isPinned_Index on NoteTable (isPinned)") ||
!sql.exec("CREATE INDEX if not exists NoteTable_Reminder_Done_Time_Index on NoteTable (reminderDoneTime)")
) {
QLOG_ERROR() << "Creation of NoteTable table failed: " << sql.lastError();
}
sql.finish();
}
//int NoteModel::rowCount(const QModelIndex & /*parent*/) const
// {
// QLOG_TRACE() << "Entering NoteModel::rowCount()";
// NSqlQuery sql(*db);
// sql.exec("Select count(lid) from NoteTable where lid in (select lid from filter)");
// if (!sql.next()) {
// QLOG_ERROR() << "Error retrieving NoteModel::rowCount: " << sql.lastError();
// return 0;
// }
// int rowCount = QVariant(sql.value(0)).toInt();
// return rowCount;
// }
int NoteModel::columnCount(const QModelIndex &parent) const {
Q_UNUSED(parent); // Suppress unused variable
// parent.column();
return NOTE_TABLE_COLUMN_COUNT;
}
Qt::ItemFlags NoteModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
// Make the table row non-editable
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
QVariant NoteModel::data (const QModelIndex & index, int role) const {
if (role == Qt::ForegroundRole) {
QString color = index.sibling(index.row(), NOTE_TABLE_COLOR_POSITION).data().toString();
if (color != "") {
if (color == "blue" || color == "black")
return QColor("white");
}
}
if (role == Qt::BackgroundRole) {
QString color = index.sibling(index.row(), NOTE_TABLE_COLOR_POSITION).data().toString();
if (color != "") {
return QColor(color);
}
}
return QSqlTableModel::data(index,role);
}
|