File indexing completed on 2024-04-21 15:07:55

0001 /*
0002  * Copyright 1999 by Martin R. Jones <mjones@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 #include "amortips.h"
0019 #include "amor_debug.h"
0020 
0021 #include <QFile>
0022 #include <QStandardPaths>
0023 #include <QRandomGenerator>
0024 
0025 #include <stdlib.h>
0026 
0027 #include <KLocalizedString>
0028 
0029 
0030 
0031 AmorTips::AmorTips()
0032 {
0033 }
0034 
0035 bool AmorTips::setFile(const QString& file)
0036 {
0037     QString path(QStandardPaths::locate(QStandardPaths::AppDataLocation, file));
0038     if (path.isEmpty()) {
0039         qDebug() << "File not found in share/amor:" << file;
0040         return false;
0041     }
0042     return read(path);
0043 }
0044 
0045 void AmorTips::reset()
0046 {
0047     mTips.clear();
0048 }
0049 
0050 QString AmorTips::tip()
0051 {
0052     if (mTips.count()) {
0053         QString tip = mTips.at( QRandomGenerator::global()->bounded( mTips.count() ) );
0054         return i18n(tip.toUtf8().constData());
0055     }
0056     return QString();
0057 }
0058 
0059 bool AmorTips::read(const QString& path)
0060 {
0061     QFile file( path );
0062 
0063     if( file.open( QIODevice::ReadOnly ) ) {
0064         while( !file.atEnd() ) {
0065             readTip( file );
0066         }
0067 
0068         qDebug() << "read" << mTips.count() << "tips";
0069         return true;
0070     }
0071 
0072     return false;
0073 }
0074 
0075 
0076 bool AmorTips::readTip(QFile &file)
0077 {
0078     char buffer[1024] = "";
0079     QString tip;
0080 
0081     while( !file.atEnd() && buffer[0] != '%' ) {
0082         file.readLine( buffer, 1024 );
0083         if( buffer[0] != '%' ) {
0084             tip += QString::fromUtf8( buffer );
0085         }
0086     }
0087 
0088     if (!tip.isEmpty()) {
0089         if (tip.endsWith(QLatin1Char('\n'))) {
0090             tip.chop(1);
0091         }
0092         mTips.append(tip);
0093         return true;
0094     }
0095 
0096     return false;
0097 }
0098 
0099 
0100 // kate: word-wrap off; encoding utf-8; indent-width 4; tab-width 4; line-numbers on; mixed-indent off; remove-trailing-space-save on; replace-tabs-save on; replace-tabs on; space-indent on;
0101 // vim:set spell et sw=4 ts=4 nowrap cino=l1,cs,U1: