Exemplo n.º 1
0
/**
* Worker thread does work
*/
double Worker::doWork()
{
    QVector<double> v, emo_vector;
    double sum = 0;
    QString title = getSongTitle(); // gets the song title

    while(songTitle->compare(title)  == 0) {
        double currentEmotion = getMood(); // gets to current mood value
        v.push_back(currentEmotion);
        if(v.size() ==  10) {
            for(int i = 0; i < v.size(); i++) {
                sum += v.at(i);
            }
            emo_vector.push_back(sum/v.size());
        }
        sleep(1);
    }

    sum = 0;
    // song finishes, compute the average
    for(int i = 0; i < emo_vector.size(); i++) {
        sum += emo_vector.at(i);
    }
    emotion = double(sum/emo_vector.size());

    // need to return the calculated value
    return emotion;
    }
Exemplo n.º 2
0
int CProxy::SubstituteTags(CString *buf, int skAccept, int searchflag, char *searchstring, int startswithflag, char *startswithstring, char *errorcode, int adminFlag)
{
	int ret = 0;
	int	iError = 0;
	char	*pszSongTitle = (char *)1;
	char	szArtist[255] = "";
	int		playlistpos = 0;
	CString	substituted;
	char	tmpbuf[1024] = "";
	int		replaceflag = 0;
	CString tempArtist;
	int		foundit = 0;
	int		loop = 1;
	int		errorCode = 0;

	errorCode = atoi(errorcode);

	ret = buf->Find("%%ARTISTSELECT%%");

	if (ret != -1) {
		while(loop) {
			memset(szArtist, '\000', sizeof(szArtist));
			loop = getArtist(playlistpos, szArtist);
			if(loop) {
				tempArtist = szArtist;
				foundit = 0;
				for(int i=0;i <= ArtistList.GetUpperBound();i++) {
					if (ArtistList.GetAt(i) == tempArtist) {
						foundit = 1;
					}
				}
				if (!foundit) {
					if (strlen(tempArtist) > 0) {	
						ArtistList.Add(tempArtist);
					}
				}
			}
			playlistpos++;
		}

		CArray<CString, CString &> ArtistListSorted;

		for(int j=0;j <= ArtistList.GetUpperBound();j++) {
			int added = 0;
			for (int k=0;k <= ArtistListSorted.GetUpperBound();k++) {
				if (ArtistList.GetAt(j) < ArtistListSorted.GetAt(k)) {
					ArtistListSorted.InsertAt(k, ArtistList.GetAt(j));
					added = 1;
					break;
				}
			}
			if (!added) {
				ArtistListSorted.Add(ArtistList.GetAt(j));
			}
		}

		for(int i=0;i <= ArtistListSorted.GetUpperBound();i++) {
			substituted = *buf;
			substituted.Replace("%%ARTISTSELECT%%", ArtistListSorted.GetAt(i));
			iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
			iError = send(skAccept, "\r\n", strlen("\r\n"), 0);

		}
		return 1;
	}

	ret = buf->Find("%%ADMINWINAMPCONTROL%%");

	if (ret != -1) {
		substituted = *buf;
		if (adminFlag) {
			char	hiddenfield[1024] = "";
			sprintf(hiddenfield, "<table bordercolor=white border=1><tr><td colspan=5>Winamp Admin Control</td></tr><tr><td><a href=\"admin.cgi?pass=%s&action=prev\">Prev</td><td><a href=\"admin.cgi?pass=%s&action=play\">Play</td><td><a href=\"admin.cgi?pass=%s&action=pause\">Pause</td><td><a href=\"admin.cgi?pass=%s&action=stop\">Stop</td><td><a href=\"admin.cgi?pass=%s&action=next\">Next</td><tr><td colspan=5><a href=\"admin.cgi?pass=%s&action=refreshcache\">Refresh Playlist Cache</td></tr></table>", g_AdminPassword, g_AdminPassword, g_AdminPassword, g_AdminPassword, g_AdminPassword, g_AdminPassword);
			substituted.Replace("%%ADMINWINAMPCONTROL%%", hiddenfield);
		}
		else {
			substituted.Replace("%%ADMINWINAMPCONTROL%%", "");
		}
		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);
		return 1;
	}

	ret = buf->Find("%%ADMINHIDDENFIELD%%");

	if (ret != -1) {
		substituted = *buf;
		if (adminFlag) {
			char	hiddenfield[1024] = "";
			sprintf(hiddenfield, "<input type=hidden name=pass value=\"%s\">", g_AdminPassword);
			substituted.Replace("%%ADMINHIDDENFIELD%%", hiddenfield);
		}
		else {
			substituted.Replace("%%ADMINHIDDENFIELD%%", "");
		}
		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);
		return 1;
	}
	ret = buf->Find("%%ADMINPASSWORD%%");

	if (ret != -1) {
		substituted = *buf;
		if (adminFlag) {
			char	hiddenfield[1024] = "";
			sprintf(hiddenfield, "&pass=%s", g_AdminPassword);
			substituted.Replace("%%ADMINPASSWORD%%", hiddenfield);
		}
		else {
			substituted.Replace("%%ADMINPASSWORD%%", "");
		}
		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);
		return 1;
	}

	ret = buf->Find("%%ERROR%%");

	if (ret != -1) {
		substituted = *buf;
		if (errorCode != 0) {
			substituted.Replace("%%ERROR%%", errorTable[errorCode]);
		}
		else {
			substituted.Replace("%%ERROR%%", "");
		}
		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);
		return 1;
	}

	ret = buf->Find("%%HTTP_REFERRER%%");

	if (ret != -1) {
		substituted = *buf;
		if (errorCode != 0) {
			substituted.Replace("%%HTTP_REFERRER%%", m_Referrer);
		}
		else {
			substituted.Replace("URL=%%HTTP_REFERRER%%", "");
			substituted.Replace("%%HTTP_REFERRER%%", "");
		}
		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);

		return 1;
	}


	playlistpos = 0;
	ret = buf->Find("%%PLAYLISTENTRY%%");

	if (ret != -1) {
		int startingPos = 0;
		if (strlen(m_Current) > 0) {
			startingPos = atoi(m_Current);
		}

		int currentPos = 0;
		numDisplayed = 0;
		maxEntries = atoi(g_MaxEntries);
		while(pszSongTitle) {
			pszSongTitle = getSongTitle(playlistpos);
			if(pszSongTitle) {
				replaceflag = 1;
				if (searchflag) {
					if (CompareNonCase(pszSongTitle, searchstring, strlen(searchstring))) {
						replaceflag = 1;
					}
					else {
						replaceflag = 0;
					}
				}
				if (startswithflag) {
					if (CompareNonCaseBeginning(pszSongTitle, startswithstring, strlen(startswithstring))) {
						replaceflag = 1;
					}
					else {
						replaceflag = 0;
					}
				}

				if (replaceflag) {
					if ((currentPos >= startingPos) && (numDisplayed < maxEntries)) {
						substituted = *buf;
						substituted.Replace("%%PLAYLISTENTRY%%", pszSongTitle);
						memset(tmpbuf, '\000', sizeof(tmpbuf));
						if (adminFlag) {
							sprintf(tmpbuf, "%d&pass=%s", playlistpos, g_AdminPassword);
						}
						else {
							sprintf(tmpbuf, "%d", playlistpos);
						}

						substituted.Replace("%%PLAYLISTPOS%%", tmpbuf);
						iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
						iError = send(skAccept, "\r\n", strlen("\r\n"), 0);
						numDisplayed++;
					}
					currentPos++;
				}
			}
			playlistpos++;
		}
		return 1;
	}
	ret = buf->Find("%%QUEUELISTENTRY%%");

	pszSongTitle = (char *)1;
	
	playlistpos = RequestQueue.GetUpperBound();

	int	queuepos = 1;
	if (ret != -1) {
		while(pszSongTitle) {
			pszSongTitle = GetQueueTitle(playlistpos);
			if(pszSongTitle) {
				substituted = *buf;
				substituted.Replace("%%QUEUELISTENTRY%%", pszSongTitle);
				memset(tmpbuf, '\000', sizeof(tmpbuf));
				if (adminFlag) {
					sprintf(tmpbuf, "<a href=\"admin.cgi?pass=%s&action=delete&listpos=%d\">Delete</a>&nbsp;%d", g_AdminPassword, queuepos, queuepos);
				}
				else {
					sprintf(tmpbuf, "%d", queuepos);
				}
				substituted.Replace("%%QUEUELISTPOS%%", tmpbuf);
				iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
				iError = send(skAccept, "\r\n", strlen("\r\n"), 0);

			}
			playlistpos--;
			queuepos++;
		}
		return 1;
	}

	ret = buf->Find("%%NAV_NEXT%%");

	if (ret != -1) {
		substituted = *buf;
		char	buff[255] = "";
		if (strchr(g_choppedrequest, '?')) {
			sprintf(buff, "<a href=\"%s&current=%d\">Next..></a>", g_choppedrequest, atoi(m_Current) + atoi(g_MaxEntries));
		}
		else {
			char	params[255] = "";

			if (startswithflag) {
				sprintf(params, "startswith=%s&current=%d", startswithstring, atoi(m_Current) + atoi(g_MaxEntries));
			}
			if (searchflag) {
				sprintf(params, "psearch=%s&current=%d", searchstring, atoi(m_Current) + atoi(g_MaxEntries));
			}
			if (strlen(params) == 0) {
				sprintf(params, "current=%d", atoi(m_Current) + atoi(g_MaxEntries));
			}
			if (adminFlag) {
				sprintf(buff, "<a href=\"admin.cgi?pass=%s&%s\">Next..></a>", g_AdminPassword, params);
			}
			else {
				sprintf(buff, "<a href=\"playlist.cgi?%s\">Next..></a>", params);
			}
		}
		if (numDisplayed < maxEntries) {
			substituted.Replace("%%NAV_NEXT%%", "");
		}
		else {
			substituted.Replace("%%NAV_NEXT%%", buff);
		}
		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);

		return 1;
	}
	ret = buf->Find("%%NAV_PREV%%");

	if (ret != -1) {
		substituted = *buf;
		char	buff[255] = "";
		if (strchr(g_choppedrequest, '?')) {
			sprintf(buff, "<a href=\"%s&current=%d\"><..Prev</a>", g_choppedrequest, atoi(m_Current) - atoi(g_MaxEntries));
		}
		else {
			char	params[255] = "";

			if (startswithflag) {
				sprintf(params, "startswith=%s&current=%d", startswithstring, atoi(m_Current) - atoi(g_MaxEntries));
			}
			if (searchflag) {
				sprintf(params, "psearch=%s&current=%d", searchstring, atoi(m_Current) - atoi(g_MaxEntries));
			}
			if (strlen(params) == 0) {
				sprintf(params, "current=%d",  atoi(m_Current) - atoi(g_MaxEntries));
			}
			if (adminFlag) {
				sprintf(buff, "<a href=\"admin.cgi?pass=%s&%s\"><...Prev</a>", g_AdminPassword, params);
			}
			else {
				sprintf(buff, "<a href=\"playlist.cgi?%s\"><...Prev</a>", params);
			}
		}
		if ((atoi(m_Current) - atoi(g_MaxEntries)) < 0) {
			substituted.Replace("%%NAV_PREV%%", "");
		}
		else {
			substituted.Replace("%%NAV_PREV%%", buff);
		}
		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);

		return 1;
	}

	ret = buf->Find("%%CURRENTSONG%%");

	if (ret != -1) {
		pszSongTitle = getCurrentSong();
		if(pszSongTitle) {
			substituted = *buf;
			substituted.Replace("%%CURRENTSONG%%", pszSongTitle);
			iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
			iError = send(skAccept, "\r\n", strlen("\r\n"), 0);

		}
		return 1;
	}

	ret = buf->Find("%%CONTENT_REFRESH%%");
	if (ret != -1) {
		substituted = *buf;
		GetSecondsLeft();
		char tmp[25] = "";
		sprintf(tmp, "%d", g_CurrentSongLeft + 5);
		
		substituted.Replace("%%CONTENT_REFRESH%%", tmp);

		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);
		return 1;
	}
	ret = buf->Find("%%PCT_LEFT%%");
	if (ret != -1) {
		substituted = *buf;
		GetSecondsLeft();
		char tmp[25] = "";
		sprintf(tmp, "%d", g_CurrentPctLeft);
		
		substituted.Replace("%%PCT_LEFT%%", tmp);

		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);
		return 1;
	}

	ret = buf->Find("%%SONG_LENGTH%%");
	if (ret != -1) {
		substituted = *buf;
		GetSecondsLeft();
		char tmp[25] = "";
		sprintf(tmp, "%d", g_CurrentSongLength);
		
		substituted.Replace("%%SONG_LENGTH%%", tmp);

		iError = send(skAccept, substituted.GetBuffer(substituted.GetLength()), substituted.GetLength(), 0);
		iError = send(skAccept, "\r\n", strlen("\r\n"), 0);
		return 1;
	}

	return 0;		
}