void test_querystring() { std::string happy("foo=bar&baz=%20"); std::map<std::string, std::string> m; int n = querystring(happy, m); assert_equal(n, 2); assert_equal(m.size(), 2); assert_equal(m["foo"], std::string("bar")); assert_equal(m["baz"], std::string(" ")); std::string sad("&"); std::map<std::string, std::string> l; n = querystring(sad, l); assert_equal(n, 0); assert_equal(l.size(), 0); }
void OcFeedsModelNew::init() { QLOG_INFO() << "Initializing feeds model"; if (!m_items.isEmpty()) clear(false); QSqlQuery query; int length = 1; QString querystring(QString("SELECT COUNT(id) FROM feeds WHERE folderId = %1").arg(folderId())); if (!query.exec(querystring)) { QLOG_ERROR() << "Feeds mode: failed to select feeds count from database: " << query.lastError().text(); } query.next(); length += query.value(0).toInt(); querystring = QString("SELECT %1 AS id, 1 AS type, '%2' AS title, (SELECT localUnreadCount FROM folders WHERE id = %1) AS unreadCount, '' AS iconSource, '' AS iconWidth, '' AS iconHeight ").arg(folderId()).arg(tr("All posts")); if (length > 1) { querystring.append("UNION "); querystring.append(QString("SELECT id, 0 AS type, title, localUnreadCount AS unreadCount, iconSource, iconWidth, iconHeight FROM feeds WHERE folderId = %1 ").arg(folderId())); } querystring.append("ORDER BY type DESC"); if (!query.exec(querystring)) { QLOG_ERROR() << "Feeds mode: failed to select feeds from database: " << query.lastError().text(); } beginInsertRows(QModelIndex(), 0, length-1); while(query.next()) { OcFeedObject *fobj = new OcFeedObject(query.value(0).toInt(), query.value(1).toInt(), query.value(2).toString(), query.value(3).toInt(), query.value(4).toString(), query.value(5).toInt(), query.value(6).toInt()); m_items.append(fobj); } endInsertRows(); }
void OcCombinedModelSql::refresh() { #ifdef QT_DEBUG qDebug() << "Refresh Combined Model"; #endif bool hideReadFeeds = dbus.getSetting("display/hidereadfeeds", false).toBool(); QSqlQuery query; QString querystring("SELECT fe.title, fe.id AS id, fe.localUnreadCount AS unreadCount, 0 AS type, fe.iconSource, fe.iconWidth, fe.iconHeight, fe.folderId, (SELECT name FROM folders WHERE id = fe.folderId) AS folderName FROM feeds fe WHERE fe.folderId > 0 "); if (hideReadFeeds) querystring.append("AND fe.localUnreadCount > 0 "); querystring.append("UNION "); querystring.append(QString("SELECT fe.title, fe.id AS id, fe.localUnreadCount AS unreadCount, 0 AS type, fe.iconSource, fe.iconWidth, fe.iconHeight, fe.folderId, '%1' AS folderName FROM feeds fe WHERE fe.folderId = 0 ").arg(tr("Uncategorized"))); if (hideReadFeeds) querystring.append("AND fe.localUnreadCount > 0 "); querystring.append("UNION "); querystring.append(QString("SELECT '%1' AS title, '0' AS id, ((SELECT IFNULL(SUM(localUnreadCount),0) FROM feeds WHERE folderId = 0) + (SELECT SUM(localUnreadCount) FROM folders)) AS unreadCount, 3 AS type, '' AS iconSource, '' AS iconWidth, '' AS iconHeight, 0 AS folderId, '' AS folderName ").arg(tr("All posts"))); query.exec(QString("SELECT id FROM items WHERE starred = %1 LIMIT 1").arg(SQL_TRUE)); if (query.next()) { querystring.append("UNION "); querystring.append(QString("SELECT '%1' AS title, '1' AS id, (SELECT COUNT(id) FROM items WHERE starred = %2) AS unreadCount, 2 AS type, '' AS iconSource, '' AS iconWidth, '' AS iconHeight, 0 AS folderId, '' AS folderName ").arg(tr("Favourite posts")).arg(SQL_TRUE)); } QString itemOrder = dbus.getSetting("display/orderby", "id").toString(); if (itemOrder == "id") { querystring.append("ORDER BY type DESC, folderId ASC, id ASC;"); } else if (itemOrder == "title") { querystring.append("ORDER BY type DESC, folderName COLLATE NOCASE ASC, title COLLATE NOCASE ASC;"); } else { querystring.append("ORDER BY type DESC, folderName COLLATE NOCASE ASC, unreadCount DESC"); } this->setQuery(querystring); }
bool ImportIconsWizard::initialLoad(QString name) { QString dirpath = GetConfDir(); QDir configDir(dirpath); if (!configDir.exists() && !configDir.mkdir(dirpath)) { LOG(VB_GENERAL, LOG_ERR, QString("Could not create %1").arg(dirpath)); } m_strChannelDir = QString("%1/%2").arg(configDir.absolutePath()) .arg("/channels"); QDir strChannelDir(m_strChannelDir); if (!strChannelDir.exists() && !strChannelDir.mkdir(m_strChannelDir)) { LOG(VB_GENERAL, LOG_ERR, QString("Could not create %1").arg(m_strChannelDir)); } m_strChannelDir += "/"; bool closeDialog = false; QString querystring("SELECT chanid, name, xmltvid, callsign," "dtv_multiplex.transportid, atsc_major_chan, " "atsc_minor_chan, dtv_multiplex.networkid, " "channel.serviceid, channel.mplexid," "dtv_multiplex.mplexid, channel.icon, channel.visible " "FROM channel LEFT JOIN dtv_multiplex " "ON channel.mplexid = dtv_multiplex.mplexid " "WHERE "); if (!name.isEmpty()) querystring.append("name=\"" + name + "\""); else querystring.append("channel.visible"); querystring.append(" ORDER BY name"); MSqlQuery query(MSqlQuery::InitCon()); query.prepare(querystring); m_listEntries.clear(); m_nCount=0; m_nMaxCount=0; m_missingMaxCount=0; if (query.exec() && query.size() > 0) { m_progressDialog = new MythUIProgressDialog(tr("Initializing, please wait..."), m_popupStack, "IconImportInitProgress"); if (m_progressDialog->Create()) { m_popupStack->AddScreen(m_progressDialog); m_progressDialog->SetTotal(query.size()); QCoreApplication::processEvents(); } else { delete m_progressDialog; m_progressDialog = NULL; } while(query.next()) { CSVEntry entry; QString relativeIconPath = query.value(11).toString(); QString absoluteIconPath = QString("%1%2").arg(m_strChannelDir) .arg(relativeIconPath); if (m_fRefresh && !relativeIconPath.isEmpty() && QFile(absoluteIconPath).exists() && !QImage(absoluteIconPath).isNull()) { LOG(VB_GENERAL, LOG_NOTICE, QString("Icon already exists, skipping (%1)").arg(absoluteIconPath)); } else { entry.strChanId=query.value(0).toString(); entry.strName=query.value(1).toString(); entry.strXmlTvId=query.value(2).toString(); entry.strCallsign=query.value(3).toString(); entry.strTransportId=query.value(4).toString(); entry.strAtscMajorChan=query.value(5).toString(); entry.strAtscMinorChan=query.value(6).toString(); entry.strNetworkId=query.value(7).toString(); entry.strServiceId=query.value(8).toString(); entry.strIconCSV= QString("%1,%2,%3,%4,%5,%6,%7,%8,%9\n"). arg(escape_csv(entry.strChanId)). arg(escape_csv(entry.strName)). arg(escape_csv(entry.strXmlTvId)). arg(escape_csv(entry.strCallsign)). arg(escape_csv(entry.strTransportId)). arg(escape_csv(entry.strAtscMajorChan)). arg(escape_csv(entry.strAtscMinorChan)). arg(escape_csv(entry.strNetworkId)). arg(escape_csv(entry.strServiceId)); entry.strNameCSV=escape_csv(entry.strName); LOG(VB_CHANNEL, LOG_INFO, QString("chanid %1").arg(entry.strIconCSV)); m_listEntries.append(entry); } m_nMaxCount++; if (m_progressDialog) { m_progressDialog->SetProgress(m_nMaxCount); QCoreApplication::processEvents(); } } if (m_progressDialog) { m_progressDialog->Close(); m_progressDialog = NULL; } } m_iter = m_listEntries.begin(); m_progressDialog = new MythUIProgressDialog( tr("Downloading, please wait..."), m_popupStack, "IconImportInitProgress"); if (m_progressDialog->Create()) { m_popupStack->AddScreen(m_progressDialog); m_progressDialog->SetTotal(m_listEntries.size()); QCoreApplication::processEvents(); } else { delete m_progressDialog; m_progressDialog = NULL; } /*: %1 is the current channel position, * %2 is the total number of channels, */ QString downloadMessage = tr("Downloading %1 of %2"); while (!closeDialog && (m_iter != m_listEntries.end())) { QString message = downloadMessage.arg(m_nCount+1) .arg(m_listEntries.size()); LOG(VB_GENERAL, LOG_NOTICE, message); if (m_missingEntries.size() > 0) { message.append("\n"); message.append(tr("Could not find %n icon(s).", "", m_missingEntries.size())); } if (!findmissing((*m_iter).strIconCSV)) { m_missingEntries.append((*m_iter)); m_missingMaxCount++; } m_nCount++; m_iter++; if (m_progressDialog) { m_progressDialog->SetMessage(message); m_progressDialog->SetProgress(m_nCount); QCoreApplication::processEvents(); } } if (m_progressDialog) { m_progressDialog->Close(); m_progressDialog = NULL; } if (m_missingEntries.size() == 0 || closeDialog) return false; if (m_nMaxCount <= 0) return false; return true; }
void php(int socket, const char *script, const char *addr, struct header *rq) { int outfd[2]; int infd[2]; int pid; char strcontentlen[BUFFER_SIZE], strmethod[BUFFER_SIZE]; char buffer[BUFFER_SIZE]; const char *qs; int p, r; pipe(outfd); pipe(infd); if (!(pid = fork())) { close(0); close(1); dup2(outfd[0], 0); dup2(infd[1], 1); snprintf(strcontentlen, BUFFER_SIZE, "%d", rq->contentlen); switch (rq->method) { case METHOD_GET: strncpy(strmethod, "GET", BUFFER_SIZE); break; case METHOD_POST: strncpy(strmethod, "POST", BUFFER_SIZE); break; case METHOD_HEAD: strncpy(strmethod, "HEAD", BUFFER_SIZE); break; default: strncpy(strmethod, "GET", BUFFER_SIZE); break; } qs = querystring(rq->uri); setenv("REDIRECT_STATUS", "1", 1); setenv("SERVER_SOFTWARE", SERVER_NAME, 1); setenv("CONTENT_LENGTH", strcontentlen, 1); setenv("REQUEST_METHOD", strmethod, 1); setenv("SCRIPT_FILENAME", script, 1); if (qs) setenv("QUERY_STRING", qs, 1); setenv("REQUEST_URI", rq->uri, 1); setenv("HTTP_HOST", rq->host, 1); setenv("REMOTE_ADDR", addr, 1); if (strlen(rq->contenttype) > 0) setenv("CONTENT_TYPE", rq->contenttype, 1); else setenv("CONTENT_TYPE", "application/x-www-form-urlencoded", 1); if (strlen(rq->cookie) > 0) setenv("HTTP_COOKIE", rq->cookie, 1); write(outfd[1], rq->payload, rq->contentlen); execl(PHP_CGI, PHP_CGI, NULL); } else { close(outfd[0]); close(outfd[1]); close(infd[1]); p = 0; do { r = read(infd[0], buffer + p, BUFFER_SIZE - p); if (r <= 0) break; p += r; } while (crlf(buffer, p) < 0); r = crlf(buffer, p); if (r >= 12 && strncmp(buffer, "Status: ", 8) == 0) { buffer[11] = 0; rq->code = atoi(buffer + 8); if (rq->code == 0) rq->code = 200; respond2(socket, rq->code, rq->protocol); writeall(socket, buffer + r + 2, p - r - 2); } else { respond2(socket, rq->code, rq->protocol); writeall(socket, buffer, p); } while ((r = read(infd[0], buffer, BUFFER_SIZE)) > 0) { writeall(socket, buffer, r); } waitpid(pid, 0, 0); } }