@ -39,11 +39,12 @@
@@ -39,11 +39,12 @@
# include <QMessageBox>
const QString Core : : CONFIG_FILE_NAME = " data " ;
const QString Core : : TOX_EXT = " .tox " ;
QList < ToxFile > Core : : fileSendQueue ;
QList < ToxFile > Core : : fileRecvQueue ;
Core : : Core ( Camera * cam , QThread * coreThread ) :
tox ( nullptr ) , camera ( cam )
Core : : Core ( Camera * cam , QThread * coreThread , QString loadPath ) :
tox ( nullptr ) , camera ( cam ) , loadPath ( loadPath )
{
videobuf = new uint8_t [ videobufsize ] ;
videoBusyness = 0 ;
@ -117,12 +118,11 @@ Core* Core::getInstance()
@@ -117,12 +118,11 @@ Core* Core::getInstance()
return Widget : : getInstance ( ) - > getCore ( ) ;
}
void Core : : start ( )
void Core : : make_tox ( )
{
// IPv6 needed for LAN discovery, but can crash some weird routers. On by default, can be disabled in options.
bool enableIPv6 = Settings : : getInstance ( ) . getEnableIPv6 ( ) ;
bool forceTCP = Settings : : getInstance ( ) . getForceTCP ( ) ;
bool useProxy = Settings : : getInstance ( ) . getUseProxy ( ) ;
if ( enableIPv6 )
@ -181,7 +181,7 @@ void Core::start()
@@ -181,7 +181,7 @@ void Core::start()
emit failedToStart ( ) ;
}
return ;
}
}
else
qWarning ( ) < < " Core failed to start with IPv6, falling back to IPv4. LAN discovery may not work properly. " ;
}
@ -205,15 +205,24 @@ void Core::start()
@@ -205,15 +205,24 @@ void Core::start()
emit failedToStart ( ) ;
return ;
}
}
void Core : : start ( )
{
make_tox ( ) ;
qsrand ( time ( nullptr ) ) ;
if ( ! loadConfiguration ( ) )
if ( loadPath ! = " " )
{
emit failedToStart ( ) ;
tox_kill ( tox ) ;
tox = nullptr ;
return ;
if ( ! loadConfiguration ( loadPath ) ) // loadPath is meaningless after this
{
emit failedToStart ( ) ;
tox_kill ( tox ) ;
tox = nullptr ;
return ;
}
loadPath = " " ;
}
tox_callback_friend_request ( tox , onFriendRequest , this ) ;
@ -273,9 +282,9 @@ void Core::start()
@@ -273,9 +282,9 @@ void Core::start()
* 5 disconnected ; 4 were DCd for less than 20 ticks , while the 5 th was ~ 50 ticks .
* So I set the tolerance here at 25 , and initial DCs should be very rare now .
* This should be able to go to 50 or 100 without affecting legitimate disconnects '
* downtime , but lets be conservative for now . Edit : now 4 0.
* downtime , but lets be conservative for now . Edit : now ~ ~ 40 ~ ~ 3 0.
*/
# define CORE_DISCONNECT_TOLERANCE 4 0
# define CORE_DISCONNECT_TOLERANCE 3 0
void Core : : process ( )
{
@ -627,8 +636,8 @@ void Core::onAvatarInfoCallback(Tox*, int32_t friendnumber, uint8_t format,
@@ -627,8 +636,8 @@ void Core::onAvatarInfoCallback(Tox*, int32_t friendnumber, uint8_t format,
{
qDebug ( ) < < " Core: Got null avatar info from " < < core - > getFriendUsername ( friendnumber ) ;
emit core - > friendAvatarRemoved ( friendnumber ) ;
QFile : : remove ( QDir ( Settings : : getInstance ( ) . get SettingsDirPath ( ) ) . filePath ( " avatars/ " + core - > getFriendAddress ( friendnumber ) . left ( 64 ) + " .png " ) ) ;
QFile : : remove ( QDir ( Settings : : getInstance ( ) . get SettingsDirPath ( ) ) . filePath ( " avatars/ " + core - > getFriendAddress ( friendnumber ) . left ( 64 ) + " .hash " ) ) ;
QFile : : remove ( QDir ( Settings : : getSettingsDirPath ( ) ) . filePath ( " avatars/ " + core - > getFriendAddress ( friendnumber ) . left ( 64 ) + " .png " ) ) ;
QFile : : remove ( QDir ( Settings : : getSettingsDirPath ( ) ) . filePath ( " avatars/ " + core - > getFriendAddress ( friendnumber ) . left ( 64 ) + " .hash " ) ) ;
}
else
{
@ -918,6 +927,8 @@ void Core::acceptFileRecvRequest(int friendId, int fileNum, QString path)
@@ -918,6 +927,8 @@ void Core::acceptFileRecvRequest(int friendId, int fileNum, QString path)
void Core : : removeFriend ( int friendId )
{
if ( ! tox )
return ;
if ( tox_del_friend ( tox , friendId ) = = - 1 ) {
emit failedToRemoveFriend ( friendId ) ;
} else {
@ -928,6 +939,8 @@ void Core::removeFriend(int friendId)
@@ -928,6 +939,8 @@ void Core::removeFriend(int friendId)
void Core : : removeGroup ( int groupId )
{
if ( ! tox )
return ;
tox_del_groupchat ( tox , groupId ) ;
}
@ -949,8 +962,8 @@ void Core::setUsername(const QString& username)
@@ -949,8 +962,8 @@ void Core::setUsername(const QString& username)
if ( tox_set_name ( tox , cUsername . data ( ) , cUsername . size ( ) ) = = - 1 ) {
emit failedToSetUsername ( username ) ;
} else {
saveConfiguration ( ) ;
emit usernameSet ( username ) ;
saveConfiguration ( ) ;
}
}
@ -981,6 +994,13 @@ ToxID Core::getSelfId()
@@ -981,6 +994,13 @@ ToxID Core::getSelfId()
return ToxID : : fromString ( CFriendAddress : : toString ( friendAddress ) ) ;
}
QString Core : : getIDString ( )
{
return getSelfId ( ) . toString ( ) . left ( 12 ) ;
// 12 is the smallest multiple of four such that
// 16^n > 10^10 (which is roughly the planet's population)
}
QString Core : : getStatusMessage ( )
{
int size = tox_get_self_status_message_size ( tox ) ;
@ -1038,11 +1058,25 @@ void Core::onFileTransferFinished(ToxFile file)
@@ -1038,11 +1058,25 @@ void Core::onFileTransferFinished(ToxFile file)
emit fileDownloadFinished ( file . filePath ) ;
}
bool Core : : loadConfiguration ( )
QString Core : : sanitize ( QString name )
{
QString path = QDir ( Settings : : getSettingsDirPath ( ) ) . filePath ( CONFIG_FILE_NAME ) ;
// these are pretty much Windows banned filename characters
QList < QChar > banned = { ' / ' , ' \\ ' , ' : ' , ' < ' , ' > ' , ' " ' , ' | ' , ' ? ' , ' * ' } ;
for ( QChar c : banned )
name . replace ( c , ' _ ' ) ;
// also remove leading and trailing periods
if ( name [ 0 ] = = ' . ' )
name [ 0 ] = ' _ ' ;
if ( name . endsWith ( ' . ' ) )
name [ name . length ( ) - 1 ] = ' _ ' ;
return name ;
}
bool Core : : loadConfiguration ( QString path )
{
// setting the profile is now the responsibility of the caller
QFile configurationFile ( path ) ;
qDebug ( ) < < " Core::loadConfiguration: reading from " < < path ;
if ( ! configurationFile . exists ( ) ) {
qWarning ( ) < < " The Tox configuration file was not found " ;
@ -1093,32 +1127,51 @@ bool Core::loadConfiguration()
@@ -1093,32 +1127,51 @@ bool Core::loadConfiguration()
void Core : : saveConfiguration ( )
{
Settings : : getInstance ( ) . save ( ) ;
QString dir = Settings : : getSettingsDirPath ( ) ;
QDir directory ( dir ) ;
if ( ! directory . exists ( ) & & ! directory . mkpath ( directory . absolutePath ( ) ) ) {
qCritical ( ) < < " Error while creating directory " < < dir ;
return ;
}
QString profile = Settings : : getInstance ( ) . getCurrentProfile ( ) ;
//qDebug() << "saveConf read profile: " << profile;
if ( profile = = " " )
{ // no profile active; this should only happen on startup, if at all
profile = sanitize ( getUsername ( ) ) ;
if ( profile = = " " ) // happens on creation of a new Tox ID
profile = getIDString ( ) ;
//qDebug() << "saveConf: read sanitized user as " << profile;
Settings : : getInstance ( ) . setCurrentProfile ( profile ) ;
}
QString path = dir + QDir : : separator ( ) + profile + TOX_EXT ;
QFileInfo info ( path ) ;
// if (!info.exists()) // fall back to old school 'data'
// { //path = dir + QDir::separator() + CONFIG_FILE_NAME;
// qDebug() << "Core:" << path << " does not exist";
// }
saveConfiguration ( path ) ;
}
void Core : : saveConfiguration ( const QString & path )
{
if ( ! tox )
{
qWarning ( ) < < " Core::saveConfiguration: Tox not started, aborting! " ;
return ;
}
QString path = Settings : : getSettingsDirPath ( ) ;
QDir directory ( path ) ;
if ( ! directory . exists ( ) & & ! directory . mkpath ( directory . absolutePath ( ) ) ) {
qCritical ( ) < < " Error while creating directory " < < path ;
return ;
}
Settings : : getInstance ( ) . save ( ) ;
path = directory . filePath ( CONFIG_FILE_NAME ) ;
QSaveFile configurationFile ( path ) ;
if ( ! configurationFile . open ( QIODevice : : WriteOnly ) ) {
qCritical ( ) < < " File " < < path < < " cannot be opened " ;
return ;
}
qDebug ( ) < < " Core: Saving " ;
qDebug ( ) < < " Core: writing tox_save to " < < path ;
uint32_t fileSize = tox_size ( tox ) ;
if ( fileSize > 0 & & fileSize < = INT32_MAX ) {
uint8_t * data = new uint8_t [ fileSize ] ;
@ -1129,6 +1182,27 @@ void Core::saveConfiguration()
@@ -1129,6 +1182,27 @@ void Core::saveConfiguration()
}
}
void Core : : switchConfiguration ( QString profile )
{
saveConfiguration ( ) ;
toxTimer - > stop ( ) ;
if ( tox ) {
toxav_kill ( toxav ) ;
toxav = nullptr ;
tox_kill ( tox ) ;
tox = nullptr ;
}
emit selfAvatarChanged ( QPixmap ( " :/img/contact_dark.png " ) ) ;
Widget : : getInstance ( ) - > clearContactsList ( ) ; // we need this to block, so no signals for us
loadPath = QDir ( Settings : : getSettingsDirPath ( ) ) . filePath ( profile + TOX_EXT ) ;
Settings : : getInstance ( ) . setCurrentProfile ( profile ) ;
start ( ) ;
}
void Core : : loadFriends ( )
{
const uint32_t friendCount = tox_count_friendlist ( tox ) ;