summaryrefslogtreecommitdiffstats
path: root/kfile-plugins/txt
diff options
context:
space:
mode:
Diffstat (limited to 'kfile-plugins/txt')
-rw-r--r--kfile-plugins/txt/Makefile.am21
-rw-r--r--kfile-plugins/txt/kfile_txt.cpp129
-rw-r--r--kfile-plugins/txt/kfile_txt.desktop70
-rw-r--r--kfile-plugins/txt/kfile_txt.h39
4 files changed, 259 insertions, 0 deletions
diff --git a/kfile-plugins/txt/Makefile.am b/kfile-plugins/txt/Makefile.am
new file mode 100644
index 0000000..84e6ce7
--- /dev/null
+++ b/kfile-plugins/txt/Makefile.am
@@ -0,0 +1,21 @@
+## Makefile.am for text file meta info plugin
+
+# set the include path for X, qt and KDE
+INCLUDES = $(all_includes)
+
+noinst_HEADERS = kfile_txt.h
+
+kde_module_LTLIBRARIES = kfile_txt.la
+
+kfile_txt_la_SOURCES = kfile_txt.cpp
+kfile_txt_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN)
+kfile_txt_la_LIBADD = $(LIB_KIO)
+
+# let automoc handle all of the meta source files (moc)
+METASOURCES = AUTO
+
+messages:
+ $(XGETTEXT) *.cpp -o $(podir)/kfile_txt.pot
+
+services_DATA = kfile_txt.desktop
+servicesdir = $(kde_servicesdir)
diff --git a/kfile-plugins/txt/kfile_txt.cpp b/kfile-plugins/txt/kfile_txt.cpp
new file mode 100644
index 0000000..02e166c
--- /dev/null
+++ b/kfile-plugins/txt/kfile_txt.cpp
@@ -0,0 +1,129 @@
+/* This file is part of the KDE project
+ * Copyright (C) 2002 Nadeem Hasan <nhasan@kde.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation version 2.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include "kfile_txt.h"
+
+#include <kgenericfactory.h>
+#include <kdebug.h>
+
+#include <qfile.h>
+#include <qstringlist.h>
+#include <qregexp.h>
+
+typedef KGenericFactory<KTxtPlugin> TxtFactory;
+
+K_EXPORT_COMPONENT_FACTORY(kfile_txt, TxtFactory("kfile_txt"))
+
+KTxtPlugin::KTxtPlugin(QObject *parent, const char *name,
+ const QStringList &args) : KFilePlugin(parent, name, args)
+{
+ kdDebug(7034) << "Text file meta info plugin\n";
+ makeMimeTypeInfo( "text/plain" );
+}
+
+void KTxtPlugin::makeMimeTypeInfo(const QString& mimeType)
+{
+ KFileMimeTypeInfo* info = addMimeTypeInfo(mimeType);
+
+ KFileMimeTypeInfo::GroupInfo* group =
+ addGroupInfo(info, "General", i18n("General"));
+
+ KFileMimeTypeInfo::ItemInfo* item;
+ item = addItemInfo(group, "Lines", i18n("Lines"), QVariant::Int);
+ setAttributes(item, KFileMimeTypeInfo::Averaged);
+ item = addItemInfo(group, "Words", i18n("Words"), QVariant::Int);
+ setAttributes(item, KFileMimeTypeInfo::Averaged);
+ item = addItemInfo(group, "Characters", i18n("Characters"), QVariant::ULongLong);
+ setAttributes(item, KFileMimeTypeInfo::Averaged);
+ item = addItemInfo(group, "Format", i18n("Format"), QVariant::String);
+}
+
+bool KTxtPlugin::readInfo(KFileMetaInfo& info, uint)
+{
+ if ( info.path().isEmpty() ) // remote file
+ return false;
+
+ QFile f(info.path());
+ if (!f.open(IO_ReadOnly))
+ return false;
+
+ bool firstline = true;
+ int totLines = 0;
+ int totWords = 0;
+ unsigned long long totChars = f.size();
+ QString fileFormat;
+ QString line;
+ bool skipTotals = (totChars > 100*1024); // 100K is the max we read
+
+ unsigned int bytesRead = 0;
+ while (!f.atEnd())
+ {
+ f.readLine(line, 4096);
+
+ int len = line.length();
+
+ // The checks below are necessary to handle embedded NULLs
+ // QFile::readLine() does not handle them well
+ bytesRead += len;
+ if (bytesRead > totChars)
+ break;
+ if (len == 0)
+ break;
+
+ if (firstline)
+ {
+ firstline = false;
+ if (line[len-1]=='\n')
+ {
+ if (len>=2 && line[len-2]=='\r')
+ fileFormat = i18n("DOS");
+ else
+ fileFormat = i18n("UNIX");
+ }
+ else if (line[len-1]=='\r')
+ fileFormat = i18n("Macintosh");
+ if (skipTotals)
+ break;
+ }
+
+ totWords += (QStringList::split(QRegExp("\\s+"), line)).count();
+ totLines++;
+ }
+
+ if (fileFormat.isEmpty())
+ fileFormat = i18n("Unknown");
+
+ kdDebug(7034) << "Lines: " << totLines << endl;
+ kdDebug(7034) << "Words: " << totWords << endl;
+ kdDebug(7034) << "Characters: " << totChars << endl;
+ kdDebug(7034) << "fileFormat: " << fileFormat << endl;
+
+ KFileMetaInfoGroup group = appendGroup(info, "General");
+ if (!skipTotals)
+ {
+ appendItem(group, "Lines", totLines);
+ appendItem(group, "Words", totWords);
+ }
+ appendItem(group, "Characters", totChars);
+ appendItem(group, "Format", fileFormat);
+
+ return true;
+}
+
+#include "kfile_txt.moc"
diff --git a/kfile-plugins/txt/kfile_txt.desktop b/kfile-plugins/txt/kfile_txt.desktop
new file mode 100644
index 0000000..2c332f3
--- /dev/null
+++ b/kfile-plugins/txt/kfile_txt.desktop
@@ -0,0 +1,70 @@
+[Desktop Entry]
+Type=Service
+Name=Text File Info
+Name[af]=Teks Lêer Inligting
+Name[ar]=معلومات ملف نصي
+Name[az]=Mətn Faylı Mə'lumatı
+Name[bg]=Информация за текстов файл
+Name[br]=Titouroù diwar-benn ar skrid restr
+Name[bs]=Info o tekst datoteci
+Name[ca]=Info. del fitxer de text
+Name[cs]=Info o textovém souboru
+Name[cy]=Gwybodaeth Ffeil Testun
+Name[da]=Information om tekstfil
+Name[de]=Informationen zur Textdatei
+Name[el]=Πληροφορίες αρχείου κειμένου
+Name[eo]=Tekstdosiera informo
+Name[es]=Información del archivo de texto
+Name[et]=Tekstifaili info
+Name[eu]=Testu fitxategiaren informazioa
+Name[fa]=اطلاعات پروندۀ متن
+Name[fi]=Tekstitiedoston tiedot
+Name[fo]=Tekstfíluupplýsingar
+Name[fr]=Informations sur le fichier texte
+Name[fy]=Teksttriem-ynfo
+Name[gl]=Información de Ficheiro de Texto
+Name[he]=מידע קובץ טקסט
+Name[hi]=पाठ फ़ाइल जानकारी
+Name[hr]=Podaci o tekstualnoj datoteci
+Name[hu]=Információ szöveges fájlokról
+Name[is]=Upplýsingar um textaskrá
+Name[it]=Informazioni File di testo
+Name[ja]=テキストファイル情報
+Name[ka]=ტექსტური ფაილის ინფორმაცია
+Name[kk]=Мәтін файлдың мәліметі
+Name[km]=ព័ត៌មាន​ឯកសារ​អត្ថបទ
+Name[lt]=Teksto bylos informacija
+Name[mk]=Информации за текстуална датотека
+Name[ms]=Maklumat Fail Teks
+Name[nb]=Tekstfilinformasjon
+Name[nds]=Textdatei-Informatschonen
+Name[ne]=पाठ फाइल सूचना
+Name[nl]=Tekstbestand-info
+Name[nn]=Informasjon om tekstfil
+Name[pa]=ਪਾਠ ਫਾਇਲ ਜਾਣਕਾਰੀ
+Name[pl]=Informacja o plikach tekstowych
+Name[pt]=Informações de Ficheiros de Texto
+Name[pt_BR]=Informações Sobre Arquivo texto
+Name[ro]=Informaţii fişier text
+Name[ru]=Информация о текстовом файле
+Name[sk]=Informácie o textovom súbore
+Name[sl]=Informacije o besedilni datoteki
+Name[sr]=Информације о текстуалном фајлу
+Name[sr@Latn]=Informacije o tekstualnom fajlu
+Name[sv]=Information om textfil
+Name[ta]=உரைக் கோப்பு தகவல்
+Name[tg]=Ахборот дар бораи файли матнӣ
+Name[th]=ข้อมูลแฟ้มข้อความ
+Name[tr]=Metin Dosyası Bilgisi
+Name[uk]=Інформація про текстовий файл
+Name[uz]=Matn fayli haqida maʼlumot
+Name[uz@cyrillic]=Матн файли ҳақида маълумот
+Name[vi]=Thông tin tập tin văn bản
+Name[xh]=Ulwazi Lombhalo Wefayile
+Name[zh_CN]=文本文件信息
+Name[zh_TW]=文字檔案資訊
+ServiceTypes=KFilePlugin
+X-KDE-Library=kfile_txt
+MimeType=text/plain
+PreferredGroups=General
+PreferredItems=Lines,Words,Characters,Format
diff --git a/kfile-plugins/txt/kfile_txt.h b/kfile-plugins/txt/kfile_txt.h
new file mode 100644
index 0000000..9b0196d
--- /dev/null
+++ b/kfile-plugins/txt/kfile_txt.h
@@ -0,0 +1,39 @@
+/* This file is part of the KDE project
+ * Copyright (C) 2002 Nadeem Hasan <nhasan@kde.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation version 2.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef __KFILE_TXT_H_
+#define __KFILE_TXT_H_
+
+#include <kfilemetainfo.h>
+
+class QStringList;
+
+class KTxtPlugin: public KFilePlugin
+{
+ Q_OBJECT
+
+public:
+ KTxtPlugin(QObject *parent, const char *name, const QStringList& args);
+ virtual bool readInfo(KFileMetaInfo& info, uint what);
+
+private:
+ void makeMimeTypeInfo(const QString& mimeType);
+};
+
+#endif