Seafood/Fishes.cpp
author User@User-PC.domain_not_set.invalid
Fri, 26 Mar 2010 15:22:34 -0700
changeset 10 79eeacfd15ff
parent 7 7ee47a65f1ad
child 11 f3dbeee07821
permissions -rw-r--r--
implement eco details with webview
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
     1
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
     2
#include <QDesktopServices>
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
     3
#include <QMessageBox>
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
     4
#include <QtGlobal>
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
     5
#include <QtSql>
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
     6
#include "Fishes.h"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
     7
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
     8
Fishes::Fishes(QObject *parent) : QObject(parent)
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
     9
{
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    10
    QSqlDatabase db;
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    11
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    12
    // Find QSLite driver
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    13
    db = QSqlDatabase::addDatabase("QSQLITE");
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    14
10
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    15
#ifdef Q_OS_SYMBIAN
3
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    16
    QString dbFile = QDesktopServices::storageLocation(QDesktopServices::DataLocation)
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    17
                     + '/'  // Qt Universal file separator
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    18
                     + "seafood.db";
10
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    19
#else
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    20
    // Windows assumed.
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    21
    // unfortunately, "C:\Documents and Settings" is corrupted on my home PC. hard coding until I fix it. -jk
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    22
    QString dbFile = "C:/workspace/QtExamples/Seafood/populateDB/seafood.db";
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    23
#endif
3
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    24
    QFile f(dbFile);
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    25
    std::string errString(dbFile.toStdString());
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    26
    if (!f.exists()) {
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    27
        qWarning("db not found ");
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    28
    } else {
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    29
        qWarning("found db ");
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    30
    }
7
John Kern <johnk@symbian.org>
parents: 3
diff changeset
    31
    qWarning(dbFile.toStdString().c_str());
3
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    32
    db.setDatabaseName(dbFile);
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    33
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    34
    // Open databasee
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    35
    if(!db.open())
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    36
    {
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    37
        std::string errCode =  db.lastError().databaseText().toStdString();
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    38
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    39
        qWarning("DB: failed to open.");
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    40
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    41
        this->bestFish << "Abalone (farmed)" << "Anchovy, European" << "Barramundi U.S."
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    42
            << "Capelin, smelt roe (Iceland)" << "Catfish (U.S.)" << "Clam, softshell"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    43
            << "Clams (farmed)" << "Cod, AK (longline)" << "Crab, Dungeness"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    44
            << "Crab, stone" << "Mussels" << "Oysters (farmed)"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    45
            << "Sablefish/ Black Cod (Alaska, Canada)" << "Salmon, wild (Alaska)"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    46
            << "Sardines, Pacific (U.S.)" << "Shrimp, pink (Oregon)"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    47
            << "Trout, rainbow (farmed)" << "Tuna, albacore (U.S., Canada)";
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    48
        this->okFish << "Clams (wild)" << "Cod, Pacific (trawl)" << "Crab, Snow"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    49
            << "Flounder/sole (Pacific)" << "Lobster, American/Maine" << "Scallops, sea (U.S., Canada)"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    50
            << "Shrimp (U.S. wild)" << "Squid" << "Tilapia (Latin America)"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    51
            << "Tuna, canned light";
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    52
        this->worstFish << "Chilean sea bass" << "Grouper" << "Orange roughy"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    53
            << "Rockfish (trawl)" << "Salmon, farmed or Atlantic" << "Shark"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    54
            << "Swordfish (imported)" << "Tilefish (Gulf of Mexico/South Atlantic)"
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    55
            << "Tuna, bigeye/yellowfin" << "Tuna, bluefin";
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    56
    } else {
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    57
        this->populate(EPresentBest);
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    58
        this->populate(EPresentOK);
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    59
        this->populate(EPresentWorst);
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    60
    }
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    61
}
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    62
10
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    63
/* given the name of a fish, return a list of eco details in html format.
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    64
 */
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    65
QString Fishes::getEcoDetails(QString name)
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    66
{
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    67
    QString detailsInHtml;
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    68
    QSqlQuery query;
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    69
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    70
    query.prepare("select details from ecoDetails "
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    71
                  "where fid in (select fid from fish where name = :name )");
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    72
    query.bindValue(":name",name);
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    73
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    74
    if (!query.exec())
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    75
    {
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    76
        QString errCode =  "failed to get eco details " + query.lastError().text();
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    77
        qWarning(errCode.toStdString().c_str());
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    78
    }
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    79
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    80
    detailsInHtml.append("<html> <title>name</title> <body> <h2>Eco Details</h2> <ul> ");
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    81
    while (query.next()){
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    82
        detailsInHtml.append( " <li>");
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    83
        detailsInHtml.append( query.value(0).toString());
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    84
        detailsInHtml.append( "</li> " );
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    85
    }
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    86
    detailsInHtml.append("</ul> </body> </html> ");
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    87
    return  detailsInHtml;
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    88
}
79eeacfd15ff implement eco details with webview
User@User-PC.domain_not_set.invalid
parents: 7
diff changeset
    89
3
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    90
void Fishes::populate(TCATEGORIES cat)
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    91
{
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    92
    QSqlQuery query;
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    93
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    94
    query.prepare("SELECT name FROM fish where category = :category ");
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    95
    query.bindValue(":category",cat);
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    96
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    97
    if (!query.exec())
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    98
    {
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
    99
        QString errCode =  "failed to populate " + query.lastError().text();
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   100
        qWarning(errCode.toStdString().c_str());
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   101
        //qFatal("Failed to add fish.");
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   102
    }
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   103
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   104
    while (query.next()){
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   105
        switch (cat)
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   106
        {
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   107
        case  EPresentBest:
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   108
            this->bestFish << query.value(0).toString();
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   109
            break;
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   110
        case EPresentWorst:
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   111
            this->worstFish << query.value(0).toString();
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   112
            break;
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   113
        case EPresentOK:
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   114
            this->okFish << query.value(0).toString();
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   115
            break;
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   116
        default:
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   117
            qWarning("this can't happen.");
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   118
            break;
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   119
        }
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   120
    }
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   121
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   122
}
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   123
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   124
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   125
const QStringList Fishes::GetBest()
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   126
{
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   127
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   128
    return bestFish;
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   129
}
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   130
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   131
const QStringList Fishes::GetOK()
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   132
{
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   133
    return okFish;
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   134
}
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   135
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   136
const QStringList Fishes::GetWorst()
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   137
{
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   138
    return worstFish;
e6d1a78b6db9 wip - start of an example for my forthcoming presentation
John Kern <johnk@symbian.org>
parents:
diff changeset
   139
}