Bug 54924

Summary: Impossible to edit shell script
Product: quanta Reporter: Miroslav Maiksnar <miroslav.maiksnar>
Component: generalAssignee: András Manţia <amantia>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version: unspecified   
Target Milestone: ---   
Platform: unspecified   
OS: Linux   
Latest Commit: Version Fixed In:

Description Miroslav Maiksnar 2003-02-20 16:14:54 UTC
Version:           3.1 (using KDE 3.1.0)
Installed from:     (3.0)
Compiler:          gcc version 2.95.4 20011002 (Debian prerelease)
OS:          Linux (i686) release 2.4.18

When text file starts with "#!/bin/sh", Quanta refuses to open it with message "Can't insert binary file as text".
Comment 1 András Manţia 2003-02-20 19:15:17 UTC
Subject: Re:  New: Impossible to edit shell script         

It's not Quanta's fault, as it checks the mimetype for every file before 
opening. If the mimetype for that files is not under the text node (e.g it 
appear only under the application node, not under application and 
text), Quanta believes that it's a binary file! But as we get more and 
more "bug" reports, I may implement a workaround. I don't close this 
report now, but remember that this is not a bug...

Andras


Comment 2 András Manţia 2003-02-26 16:23:21 UTC
Subject: QUANTA_3_1_BRANCH: quanta

CVS commit by amantia: 

Backport KMimeType::findFormatByFileContent(), which fixes the unnecessary warning about a file being a binary one, even if it was a text file, but its mimetype was not under the "text" node. It needs also the updated mimetypes.

CCMAIL: 54924-done@bugs.kde.org


  M +2 -0      ChangeLog   1.90.2.31
  M +31 -0     quanta/quantacommon.cpp   1.29.2.3
  M +6 -0      quanta/quantacommon.h   1.26.2.4


--- quanta/ChangeLog  #1.90.2.30:1.90.2.31
@@ -22,4 +22,6 @@
     - saving of Author and E-Mail project options was broken in some cases
     - fix numbering of new documents
+    - do not complain about text files being binary ones on a system with broken
+      mimetypes [#54924]
   - usability enhanchements:
     - show the tag attributes (Alt-Down) menu lower than the current line

--- quanta/quanta/quantacommon.cpp  #1.29.2.2:1.29.2.3
@@ -340,4 +340,11 @@ bool QuantaCommon::checkMimeGroup(const 
     }
  }
+ if (!status && group == "text" && url.isLocalFile())
+ {
+   Format f = findFormatByFileContent(url.path());
+   if (f.text && f.compression == Format::NoCompression)
+       status = true;
+ }
+
 
  return status;
@@ -430,2 +437,26 @@ QString QuantaCommon::i18n2normal(const 
 }
 
+#define GZIP_MAGIC1     0x1f
+#define GZIP_MAGIC2     0x8b
+
+Format QuantaCommon::findFormatByFileContent( const QString &fileName )
+{
+  Format result;
+  result.compression = Format::NoCompression;
+  KMimeType::Ptr mime = KMimeType::findByFileContent(fileName);
+
+  result.text = mime->name().startsWith("text/");
+  QVariant v = mime->property("X-KDE-text");
+  if (v.isValid())
+     result.text = v.toBool();
+
+  QFile f(fileName);
+  if (f.open(IO_ReadOnly))
+  {
+     unsigned char buf[10+1];
+     int l = f.readBlock((char *)buf, 10);
+     if ((l > 2) && (buf[0] == GZIP_MAGIC1) && (buf[1] == GZIP_MAGIC2))
+        result.compression = Format::GZipCompression;
+  }
+  return result;
+}

--- quanta/quanta/quantacommon.h  #1.26.2.3:1.26.2.4
@@ -73,4 +73,9 @@ typedef struct QConfig{
         };
 
+typedef struct {
+     bool text : 1;
+     enum { NoCompression=0, GZipCompression } compression : 4;
+     int dummy : 27;
+  } Format;
 
 /**Some common, mostly static functions.
@@ -133,4 +138,5 @@ pointer must be deleted by the caller!! 
   static QString i18n2normal(const QString& a_str);
 
+  static Format findFormatByFileContent( const QString &fileName );
 };