Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcontenthistory.h"
Toshihiro Shimizu 890ddd
#include <qstring></qstring>
Toshihiro Shimizu 890ddd
#include <qdatetime></qdatetime>
Toshihiro Shimizu 890ddd
#include <qstringlist></qstringlist>
Toshihiro Shimizu 890ddd
#include <qprocess></qprocess>
Toshihiro Shimizu 890ddd
#include <qtextstream></qtextstream>
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#include <map></map>
Toshihiro Shimizu 890ddd
#include <qfile></qfile>
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
class HistoryRec
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
QDateTime m_date;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
HistoryRec(const QDateTime&date QString&user, const QString&machine)
Toshihiro Shimizu 890ddd
 : date(date)
Toshihiro Shimizu 890ddd
 , m_user
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TContentHistory::TContentHistory(bool isLevel)
Toshihiro Shimizu 890ddd
	: m_isLevel(isLevel), m_frozenHistory()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TContentHistory::~TContentHistory()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TContentHistory *TContentHistory::clone() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TContentHistory *history = new TContentHistory(m_isLevel);
Toshihiro Shimizu 890ddd
	history->deserialize(serialize());
Toshihiro Shimizu 890ddd
	return history;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TContentHistory::deserialize(const QString ¤tHistory)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_frozenHistory = currentHistory;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
QStringList strlist = str.split("#");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//skip strlist[0], is the header
Toshihiro Shimizu 890ddd
int i;
Toshihiro Shimizu 890ddd
for (i=1; i
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  QString str = strlist.at(i);
Toshihiro Shimizu 890ddd
  parseLine(&str);
Toshihiro Shimizu 890ddd
   }
Toshihiro Shimizu 890ddd
}*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline QString blanks(const QString &str, int count = 15)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString res = str;
Toshihiro Shimizu 890ddd
	if (res.size() >= count)
Toshihiro Shimizu 890ddd
		res.truncate(count);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		res += QString(count - str.size(), ' ');
Toshihiro Shimizu 890ddd
	return res;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline QString getStr(const TFrameId &id)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (id.getLetter() != 0)
Toshihiro Shimizu 890ddd
		return QString::number(id.getNumber()) + id.getLetter();
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return QString::number(id.getNumber());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const QString Fmt = "dd MMM yy   hh:mm";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString getLine(int counter, const QDateTime &date, const set<tframeid> &frames)</tframeid>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static QString user;
Toshihiro Shimizu 890ddd
	static QString machine;
Toshihiro Shimizu 890ddd
	if (user == "") {
Toshihiro Shimizu 890ddd
		QStringList list = QProcess::systemEnvironment();
Toshihiro Shimizu 890ddd
		int j;
Toshihiro Shimizu 890ddd
		for (j = 0; j < list.size(); j++) {
Toshihiro Shimizu 890ddd
			QString value = list.at(j);
Toshihiro Shimizu 890ddd
			if (value.startsWith("USERNAME="))
Toshihiro Shimizu 890ddd
				user = blanks(value.right(value.size() - 9));
Toshihiro Shimizu 890ddd
			else if (value.startsWith("COMPUTERNAME="))
Toshihiro Shimizu 890ddd
				machine = blanks(value.right(value.size() - 13));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (frames.empty())
Toshihiro Shimizu 890ddd
		return "| #" + blanks(QString::number(counter), 4) + blanks(date.toString(Fmt), 20) + blanks(machine, 12) + user + " |";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString framesStr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	set<tframeid>::const_iterator it = frames.begin();</tframeid>
Toshihiro Shimizu 890ddd
	TFrameId first, last;
Toshihiro Shimizu 890ddd
	while (it != frames.end()) {
Toshihiro Shimizu 890ddd
		first = last = *it;
Toshihiro Shimizu 890ddd
		++it;
Toshihiro Shimizu 890ddd
		while (it != frames.end() &&
Toshihiro Shimizu 890ddd
			   ((*it).getNumber() == last.getNumber() || // 1a, 1b....
Toshihiro Shimizu 890ddd
				(*it).getNumber() == last.getNumber() + 1)) {
Toshihiro Shimizu 890ddd
			assert(*it > last);
Toshihiro Shimizu 890ddd
			last = *it, ++it;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		framesStr += getStr(first) + ((first != last) ? "-" + getStr(last) : "");
Toshihiro Shimizu 890ddd
		if (it != frames.end())
Toshihiro Shimizu 890ddd
			framesStr += ", ";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return "| #" + blanks(QString::number(counter), 4) + blanks(date.toString(Fmt), 20) + blanks(machine, 12) + user + blanks(framesStr, 20) + " |";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int getCurrentCount(const QString &str)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (str == "")
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int from = str.lastIndexOf('#') + 1;
Toshihiro Shimizu 890ddd
	assert(from != -1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int to = str.indexOf(" ", from) - 1;
Toshihiro Shimizu 890ddd
	assert(to != -1);
Toshihiro Shimizu 890ddd
	assert(from <= to);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (str.mid(from, to - from + 1)).toInt();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const QString TContentHistory::currentToString() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_records.empty())
Toshihiro Shimizu 890ddd
		return "";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int counter = getCurrentCount(m_frozenHistory);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!m_isLevel) {
Toshihiro Shimizu 890ddd
		assert(m_records.size() == 1);
Toshihiro Shimizu 890ddd
		return getLine(++counter, m_records.begin()->second, set<tframeid>());</tframeid>
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString out;
Toshihiro Shimizu 890ddd
	std::multimap<qdatetime, tframeid=""> dateSorted;</qdatetime,>
Toshihiro Shimizu 890ddd
	std::map<tframeid, qdatetime="">::const_iterator it;</tframeid,>
Toshihiro Shimizu 890ddd
	for (it = m_records.begin(); it != m_records.end(); ++it)
Toshihiro Shimizu 890ddd
		dateSorted.insert(pair<qdatetime, tframeid="">(it->second, it->first));</qdatetime,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::multimap<qdatetime, tframeid="">::const_iterator it1 = dateSorted.begin();</qdatetime,>
Toshihiro Shimizu 890ddd
	QDateTime currDate = it1->first;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (it1 != dateSorted.end()) {
Toshihiro Shimizu 890ddd
		set<tframeid> frames;</tframeid>
Toshihiro Shimizu 890ddd
		while (it1 != dateSorted.end() && currDate == it1->first) {
Toshihiro Shimizu 890ddd
			frames.insert(it1->second);
Toshihiro Shimizu 890ddd
			++it1;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		assert(!frames.empty());
Toshihiro Shimizu 890ddd
		out += getLine(++counter, currDate, frames);
Toshihiro Shimizu 890ddd
		if (it1 != dateSorted.end())
Toshihiro Shimizu 890ddd
			currDate = it1->first;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return out;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const QString TContentHistory::serialize() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	const QString currentHistory = currentToString();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_frozenHistory != "")
Toshihiro Shimizu 890ddd
		return m_frozenHistory + currentHistory;
Toshihiro Shimizu 890ddd
	else if (currentHistory != "") {
Toshihiro Shimizu 890ddd
		if (m_isLevel)
Toshihiro Shimizu 890ddd
			return "| #    DATE:       Time:   MACHINE:    USER:          FRAMES MODIFIED:     |" + currentHistory;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			return "| #    DATE:       Time:   MACHINE:    USER:           |" + currentHistory;
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		return "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TContentHistory::fixCurrentHistory()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_frozenHistory = serialize();
Toshihiro Shimizu 890ddd
	m_records.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TContentHistory::frameRangeModifiedNow(const TFrameId &fromId, const TFrameId &toId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_isLevel);
Toshihiro Shimizu 890ddd
	QDateTime date = QDateTime::currentDateTime();
Toshihiro Shimizu 890ddd
	QDateTime dateNoSecs(date.date(), date.time().addSecs(-date.time().second()));
Toshihiro Shimizu 890ddd
	assert(dateNoSecs.secsTo(date) == date.time().second());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	m_records[fromId] = dateNoSecs;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (fromId == toId)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = fromId.getNumber() + 1; i <= toId.getNumber() - 1; i++)
Toshihiro Shimizu 890ddd
		m_records[TFrameId(i)] = dateNoSecs;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_records[toId] = dateNoSecs;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TContentHistory::modifiedNow()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(!m_isLevel);
Toshihiro Shimizu 890ddd
	QDateTime date = QDateTime::currentDateTime();
Toshihiro Shimizu 890ddd
	QDateTime dateNoSecs(date.date(), date.time().addSecs(-date.time().second()));
Toshihiro Shimizu 890ddd
	assert(dateNoSecs.secsTo(date) == date.time().second());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_records[0] = dateNoSecs;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
void testHistory()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
		TContentHistory ch(true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ch.frameModifiedNow(TFrameId(13));
Toshihiro Shimizu 890ddd
		Sleep(1000);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ch.frameModifiedNow(TFrameId(3, 'c'));
Toshihiro Shimizu 890ddd
		ch.frameRangeModifiedNow(TFrameId(5), TFrameId(7));
Toshihiro Shimizu 890ddd
		ch.fixCurrentHistory();
Toshihiro Shimizu 890ddd
		Sleep(2000);
Toshihiro Shimizu 890ddd
		ch.frameRangeModifiedNow(TFrameId(6), TFrameId(9));
Toshihiro Shimizu 890ddd
		ch.frameModifiedNow(TFrameId(11));
Toshihiro Shimizu 890ddd
		QString str1 = ch.serialize();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TContentHistory ch1(true);
Toshihiro Shimizu 890ddd
		ch1.deserialize(str1);
Toshihiro Shimizu 890ddd
		ch1.frameRangeModifiedNow(TFrameId(2), TFrameId(3, 'c'));
Toshihiro Shimizu 890ddd
		Sleep(2000);
Toshihiro Shimizu 890ddd
		ch1.frameModifiedNow(TFrameId(3, 'a'));
Toshihiro Shimizu 890ddd
		ch1.fixCurrentHistory();
Toshihiro Shimizu 890ddd
		Sleep(2000);
Toshihiro Shimizu 890ddd
		ch1.frameModifiedNow(TFrameId(11, 'b'));
Toshihiro Shimizu 890ddd
		ch1.frameModifiedNow(TFrameId(12));
Toshihiro Shimizu 890ddd
		QString str2 = ch1.serialize();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		QFile f("C:\\temp\\out.txt");
Toshihiro Shimizu 890ddd
		f.open(QIODevice::WriteOnly | QIODevice::Text);
Toshihiro Shimizu 890ddd
		QTextStream out(&f);
Toshihiro Shimizu 890ddd
		out << str2;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
    {
Toshihiro Shimizu 890ddd
		TContentHistory ch(false);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ch.modifiedNow();
Toshihiro Shimizu 890ddd
		Sleep(1000);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ch.modifiedNow();
Toshihiro Shimizu 890ddd
		ch.modifiedNow();
Toshihiro Shimizu 890ddd
		ch.fixCurrentHistory();
Toshihiro Shimizu 890ddd
		Sleep(2000);
Toshihiro Shimizu 890ddd
		ch.modifiedNow();
Toshihiro Shimizu 890ddd
		ch.modifiedNow();
Toshihiro Shimizu 890ddd
		QString str1 = ch.serialize();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TContentHistory ch1(false);
Toshihiro Shimizu 890ddd
		ch1.deserialize(str1);
Toshihiro Shimizu 890ddd
		ch1.modifiedNow();
Toshihiro Shimizu 890ddd
		Sleep(2000);
Toshihiro Shimizu 890ddd
		ch1.modifiedNow();
Toshihiro Shimizu 890ddd
		ch1.fixCurrentHistory();
Toshihiro Shimizu 890ddd
		Sleep(2000);
Toshihiro Shimizu 890ddd
		ch1.modifiedNow();
Toshihiro Shimizu 890ddd
		ch1.modifiedNow();
Toshihiro Shimizu 890ddd
		QString str2 = ch1.serialize();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		QFile f("C:\\temp\\out1.txt");
Toshihiro Shimizu 890ddd
		f.open(QIODevice::WriteOnly | QIODevice::Text);
Toshihiro Shimizu 890ddd
		QTextStream out(&f);
Toshihiro Shimizu 890ddd
		out << str2;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/