Qt 4.8
Classes | Functions
qftp.cpp File Reference
#include "qftp.h"
#include "qabstractsocket.h"
#include "qcoreapplication.h"
#include "qtcpsocket.h"
#include "qurlinfo.h"
#include "qstringlist.h"
#include "qregexp.h"
#include "qtimer.h"
#include "qfileinfo.h"
#include "qhash.h"
#include "qtcpserver.h"
#include "qlocale.h"
#include <private/qobject_p.h>
#include "qftp.moc"
#include "moc_qftp.cpp"

Go to the source code of this file.

Classes

class  QFtpCommand
 
class  QFtpDTP
 
class  QFtpPI
 
class  QFtpPrivate
 

Functions

static void _q_fixupDateTime (QDateTime *dateTime, bool leapYear=false)
 
static void _q_parseDosDir (const QStringList &tokens, const QString &userName, QUrlInfo *info)
 
static void _q_parseUnixDir (const QStringList &tokens, const QString &userName, QUrlInfo *info)
 

Function Documentation

◆ _q_fixupDateTime()

static void _q_fixupDateTime ( QDateTime dateTime,
bool  leapYear = false 
)
static

Definition at line 469 of file qftp.cpp.

Referenced by _q_parseUnixDir().

470 {
471  // Adjust for future tolerance.
472  const int futureTolerance = 86400;
473  if (dateTime->secsTo(QDateTime::currentDateTime()) < -futureTolerance) {
474  QDate d = dateTime->date();
475  if (leapYear) {
476  int prevLeapYear = d.year() - 1;
477 
478  while (!QDate::isLeapYear(prevLeapYear))
479  prevLeapYear--;
480 
481  d.setYMD(prevLeapYear, d.month(), d.day());
482  } else {
483  d.setYMD(d.year() - 1, d.month(), d.day());
484  }
485  dateTime->setDate(d);
486  }
487 }
double d
Definition: qnumeric_p.h:62
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
The QDate class provides date functions.
Definition: qdatetime.h:55
static bool isLeapYear(int year)
Returns true if the specified year is a leap year; otherwise returns false.
Definition: qdatetime.cpp:1404
void setDate(const QDate &date)
Sets the date part of this datetime to date.
Definition: qdatetime.cpp:2399
int secsTo(const QDateTime &) const
Returns the number of seconds from this datetime to the other datetime.
Definition: qdatetime.cpp:2914
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
static QDateTime currentDateTime()
Returns the current datetime, as reported by the system clock, in the local time zone.
Definition: qdatetime.cpp:3138
bool setYMD(int y, int m, int d)
Sets the date&#39;s year y, month m, and day d.
Definition: qdatetime.cpp:937
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353

◆ _q_parseDosDir()

static void _q_parseDosDir ( const QStringList tokens,
const QString userName,
QUrlInfo info 
)
static

Definition at line 598 of file qftp.cpp.

Referenced by QFtpDTP::parseDir().

599 {
600  // DOS style, 3 + 1 entries
601  // 01-16-02 11:14AM <DIR> epsgroup
602  // 06-05-03 03:19PM 1973 readme.txt
603  if (tokens.size() != 4)
604  return;
605 
606  Q_UNUSED(userName);
607 
608  QString name = tokens.at(3);
609  info->setName(name);
610  info->setSymLink(name.toLower().endsWith(QLatin1String(".lnk")));
611 
612  if (tokens.at(2) == QLatin1String("<DIR>")) {
613  info->setFile(false);
614  info->setDir(true);
615  } else {
616  info->setFile(true);
617  info->setDir(false);
618  info->setSize(tokens.at(2).toLongLong());
619  }
620 
621  // Note: We cannot use QFileInfo; permissions are for the server-side
622  // machine, and QFileInfo's behavior depends on the local platform.
623  int permissions = QUrlInfo::ReadOwner | QUrlInfo::WriteOwner
626  QString ext;
627  int extIndex = name.lastIndexOf(QLatin1Char('.'));
628  if (extIndex != -1)
629  ext = name.mid(extIndex + 1);
630  if (ext == QLatin1String("exe") || ext == QLatin1String("bat") || ext == QLatin1String("com"))
632  info->setPermissions(permissions);
633 
634  info->setReadable(true);
635  info->setWritable(info->isFile());
636 
637  QDateTime dateTime;
638 #ifndef QT_NO_DATESTRING
639  dateTime = QLocale::c().toDateTime(tokens.at(1), QLatin1String("MM-dd-yy hh:mmAP"));
640  if (dateTime.date().year() < 1971) {
641  dateTime.setDate(QDate(dateTime.date().year() + 100,
642  dateTime.date().month(),
643  dateTime.date().day()));
644  }
645 #endif
646 
647  info->setLastModified(dateTime);
648 
649 }
virtual void setSize(qint64 size)
Specifies the size of the URL.
Definition: qurlinfo.cpp:374
virtual void setFile(bool b)
If b is true then the URL is set to be a file; if is false then the URL is set not to be a file (whic...
Definition: qurlinfo.cpp:272
qlonglong toLongLong(bool *ok=0, int base=10) const
Returns the string converted to a long long using base base, which is 10 by default and must be betwe...
Definition: qstring.cpp:5943
bool isFile() const
Returns true if the URL is a file; otherwise returns false.
Definition: qurlinfo.cpp:573
QDateTime toDateTime(const QString &string, FormatType format=LongFormat) const
Parses the date/time string given in string and returns the time.
Definition: qlocale.cpp:1649
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
The QDate class provides date functions.
Definition: qdatetime.h:55
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual void setDir(bool b)
If b is true then the URL is set to be a directory; if b is false then the URL is set not to be a dir...
Definition: qurlinfo.cpp:252
virtual void setName(const QString &name)
Sets the name of the URL to name.
Definition: qurlinfo.cpp:232
virtual void setPermissions(int p)
Specifies that the URL has access permissions p.
Definition: qurlinfo.cpp:390
void setDate(const QDate &date)
Sets the date part of this datetime to date.
Definition: qdatetime.cpp:2399
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
static QLocale c()
Returns a QLocale object initialized to the "C" locale.
Definition: qlocale.h:773
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
virtual void setSymLink(bool b)
Specifies that the URL refers to a symbolic link if b is true and that it does not if b is false...
Definition: qurlinfo.cpp:290
int lastIndexOf(QChar c, int from=-1, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:3000
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
virtual void setWritable(bool b)
Specifies that the URL is writable if b is true and not writable if b is false.
Definition: qurlinfo.cpp:308
virtual void setLastModified(const QDateTime &dt)
Specifies that the object the URL refers to was last modified at dt.
Definition: qurlinfo.cpp:407
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
virtual void setReadable(bool b)
Specifies that the URL is readable if b is true and not readable if b is false.
Definition: qurlinfo.cpp:326
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ _q_parseUnixDir()

static void _q_parseUnixDir ( const QStringList tokens,
const QString userName,
QUrlInfo info 
)
static

Definition at line 489 of file qftp.cpp.

Referenced by QFtpDTP::parseDir().

490 {
491  // Unix style, 7 + 1 entries
492  // -rw-r--r-- 1 ftp ftp 17358091 Aug 10 2004 qt-x11-free-3.3.3.tar.gz
493  // drwxr-xr-x 3 ftp ftp 4096 Apr 14 2000 compiled-examples
494  // lrwxrwxrwx 1 ftp ftp 9 Oct 29 2005 qtscape -> qtmozilla
495  if (tokens.size() != 8)
496  return;
497 
498  char first = tokens.at(1).at(0).toLatin1();
499  if (first == 'd') {
500  info->setDir(true);
501  info->setFile(false);
502  info->setSymLink(false);
503  } else if (first == '-') {
504  info->setDir(false);
505  info->setFile(true);
506  info->setSymLink(false);
507  } else if (first == 'l') {
508  info->setDir(true);
509  info->setFile(false);
510  info->setSymLink(true);
511  }
512 
513  // Resolve filename
514  QString name = tokens.at(7);
515  if (info->isSymLink()) {
516  int linkPos = name.indexOf(QLatin1String(" ->"));
517  if (linkPos != -1)
518  name.resize(linkPos);
519  }
520  info->setName(name);
521 
522  // Resolve owner & group
523  info->setOwner(tokens.at(3));
524  info->setGroup(tokens.at(4));
525 
526  // Resolve size
527  info->setSize(tokens.at(5).toLongLong());
528 
529  QStringList formats;
530  formats << QLatin1String("MMM dd yyyy") << QLatin1String("MMM dd hh:mm") << QLatin1String("MMM d yyyy")
531  << QLatin1String("MMM d hh:mm") << QLatin1String("MMM d yyyy") << QLatin1String("MMM dd yyyy");
532 
533  QString dateString = tokens.at(6);
534  dateString[0] = dateString[0].toUpper();
535 
536  // Resolve the modification date by parsing all possible formats
537  QDateTime dateTime;
538  int n = 0;
539 #ifndef QT_NO_DATESTRING
540  do {
541  dateTime = QLocale::c().toDateTime(dateString, formats.at(n++));
542  } while (n < formats.size() && (!dateTime.isValid()));
543 #endif
544 
545  if (n == 2 || n == 4) {
546  // Guess the year.
547  dateTime.setDate(QDate(QDate::currentDate().year(),
548  dateTime.date().month(),
549  dateTime.date().day()));
550  _q_fixupDateTime(&dateTime);
551  }
552  if (dateTime.isValid())
553  info->setLastModified(dateTime);
554  else if (dateString.startsWith(QLatin1String("Feb 29"))) {
555 
556  // When the current year on the FTP server is a leap year and a
557  // file's last modified date is Feb 29th, and the current day on
558  // the FTP server is also Feb 29th, then the date can be in
559  // formats n==2 or n==4. toDateTime in that case defaults to 1900
560  // for the missing year. Feb 29 1900 is an invalid date and so
561  // wont be parsed. This adds an exception that handles it.
562 
563  int recentLeapYear;
564  QString timeString = dateString.mid(7);
565 
566  dateTime = QLocale::c().toDateTime(timeString, QLatin1String("hh:mm"));
567 
568  recentLeapYear = QDate::currentDate().year();
569 
570  while (!QDate::isLeapYear(recentLeapYear))
571  recentLeapYear--;
572 
573  dateTime.setDate(QDate(recentLeapYear, 2, 29));
574 
575  _q_fixupDateTime(&dateTime, true);
576  info->setLastModified(dateTime);
577  }
578 
579  // Resolve permissions
580  int permissions = 0;
581  QString p = tokens.at(2);
582  permissions |= (p[0] == QLatin1Char('r') ? QUrlInfo::ReadOwner : 0);
583  permissions |= (p[1] == QLatin1Char('w') ? QUrlInfo::WriteOwner : 0);
584  permissions |= (p[2] == QLatin1Char('x') ? QUrlInfo::ExeOwner : 0);
585  permissions |= (p[3] == QLatin1Char('r') ? QUrlInfo::ReadGroup : 0);
586  permissions |= (p[4] == QLatin1Char('w') ? QUrlInfo::WriteGroup : 0);
587  permissions |= (p[5] == QLatin1Char('x') ? QUrlInfo::ExeGroup : 0);
588  permissions |= (p[6] == QLatin1Char('r') ? QUrlInfo::ReadOther : 0);
589  permissions |= (p[7] == QLatin1Char('w') ? QUrlInfo::WriteOther : 0);
590  permissions |= (p[8] == QLatin1Char('x') ? QUrlInfo::ExeOther : 0);
591  info->setPermissions(permissions);
592 
593  bool isOwner = info->owner() == userName;
594  info->setReadable((permissions & QUrlInfo::ReadOther) || ((permissions & QUrlInfo::ReadOwner) && isOwner));
595  info->setWritable((permissions & QUrlInfo::WriteOther) || ((permissions & QUrlInfo::WriteOwner) && isOwner));
596 }
virtual void setSize(qint64 size)
Specifies the size of the URL.
Definition: qurlinfo.cpp:374
virtual void setFile(bool b)
If b is true then the URL is set to be a file; if is false then the URL is set not to be a file (whic...
Definition: qurlinfo.cpp:272
QString owner() const
Returns the owner of the URL.
Definition: qurlinfo.cpp:495
qlonglong toLongLong(bool *ok=0, int base=10) const
Returns the string converted to a long long using base base, which is 10 by default and must be betwe...
Definition: qstring.cpp:5943
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QDateTime toDateTime(const QString &string, FormatType format=LongFormat) const
Parses the date/time string given in string and returns the time.
Definition: qlocale.cpp:1649
QString toUpper() const Q_REQUIRED_RESULT
Returns an uppercase copy of the string.
Definition: qstring.cpp:5483
virtual void setGroup(const QString &s)
Specifies that the owning group of the URL is called s.
Definition: qurlinfo.cpp:358
int month() const
Returns the number corresponding to the month of this date, using the following convention: ...
Definition: qdatetime.cpp:382
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
int day() const
Returns the day of the month (1 to 31) of this date.
Definition: qdatetime.cpp:395
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
bool isSymLink() const
Returns true if the URL is a symbolic link; otherwise returns false.
Definition: qurlinfo.cpp:586
The QDate class provides date functions.
Definition: qdatetime.h:55
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual void setDir(bool b)
If b is true then the URL is set to be a directory; if b is false then the URL is set not to be a dir...
Definition: qurlinfo.cpp:252
virtual void setOwner(const QString &s)
Specifies that the owner of the URL is called s.
Definition: qurlinfo.cpp:342
static bool isLeapYear(int year)
Returns true if the specified year is a leap year; otherwise returns false.
Definition: qdatetime.cpp:1404
virtual void setName(const QString &name)
Sets the name of the URL to name.
Definition: qurlinfo.cpp:232
virtual void setPermissions(int p)
Specifies that the URL has access permissions p.
Definition: qurlinfo.cpp:390
void setDate(const QDate &date)
Sets the date part of this datetime to date.
Definition: qdatetime.cpp:2399
static QDate currentDate()
Returns the current date, as reported by the system clock.
Definition: qdatetime.cpp:3115
const char * name
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
void resize(int size)
Sets the size of the string to size characters.
Definition: qstring.cpp:1353
QDate date() const
Returns the date part of the datetime.
Definition: qdatetime.cpp:2357
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
static void _q_fixupDateTime(QDateTime *dateTime, bool leapYear=false)
Definition: qftp.cpp:469
static QLocale c()
Returns a QLocale object initialized to the "C" locale.
Definition: qlocale.h:773
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
virtual void setSymLink(bool b)
Specifies that the URL refers to a symbolic link if b is true and that it does not if b is false...
Definition: qurlinfo.cpp:290
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
char toLatin1() const
Returns the Latin-1 character equivalent to the QChar, or 0.
Definition: qchar.h:376
virtual void setWritable(bool b)
Specifies that the URL is writable if b is true and not writable if b is false.
Definition: qurlinfo.cpp:308
virtual void setLastModified(const QDateTime &dt)
Specifies that the object the URL refers to was last modified at dt.
Definition: qurlinfo.cpp:407
int year() const
Returns the year of this date.
Definition: qdatetime.cpp:353
virtual void setReadable(bool b)
Specifies that the URL is readable if b is true and not readable if b is false.
Definition: qurlinfo.cpp:326
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55