commit 035bc0a0e3f6360146b533289a14100b1d158341
Author: Przemysław Buczkowski <przemub@yahoo.pl>
Date:   Thu Jan 10 19:24:19 2013 +0100

    in progress

diff --git a/src/apps/stylededit/StyledEditWindow.cpp b/src/apps/stylededit/StyledEditWindow.cpp
index 3193bcf..55c15e7 100644
--- a/src/apps/stylededit/StyledEditWindow.cpp
+++ b/src/apps/stylededit/StyledEditWindow.cpp
@@ -31,6 +31,7 @@
 #include <Debug.h>
 #include <File.h>
 #include <FilePanel.h>
+#include <FindDirectory.h>
 #include <fs_attr.h>
 #include <Locale.h>
 #include <Menu.h>
@@ -46,6 +47,8 @@
 #include <TextControl.h>
 #include <TextView.h>
 #include <TranslationUtils.h>
+#include <TranslatorFormats.h>
+#include <TranslatorRoster.h>
 #include <UnicodeChar.h>
 
 
@@ -764,7 +767,61 @@ StyledEditWindow::Save(BMessage* message)
 				}
 			}
 
-			status = fTextView->WriteStyledEditFile(&file);
+			if (fSavePanel != NULL &&
+				fSavePanelFormatMenu->IndexOf(fSavePanelFormatMenu->FindMarked()) != 0) {
+				int selected = fSavePanelFormatMenu->IndexOf(fSavePanelFormatMenu->FindMarked());
+
+				BTranslatorRoster* roster = new BTranslatorRoster;
+				roster->AddTranslators(NULL);
+
+				const translation_format* translator_out;
+				roster->GetOutputFormats(fSTXTTranslators[selected],
+					&translator_out, NULL);
+
+				BPath cachePath;
+				status = find_directory(B_COMMON_CACHE_DIRECTORY, &cachePath);
+				BDirectory cacheDir(cachePath.Path());
+
+				BEntry cacheEntry(&cacheDir, ".stylededitcache");
+				BFile cache(&cacheEntry, B_READ_WRITE | B_CREATE_FILE);
+
+				if (cacheEntry.InitCheck() != B_OK) {
+					BString alertText;
+					bs_printf(&alertText, B_TRANSLATE(
+						"Error creating cache file for export \"%s\":\n%s"),
+						name, strerror(status));
+
+					_ShowAlert(alertText, B_TRANSLATE("OK"), "", "", B_STOP_ALERT);
+					return status;
+				}
+
+				status = fTextView->WriteStyledEditFile(&cache);
+
+				if (status != B_OK) {
+					BString alertText;
+					bs_printf(&alertText, B_TRANSLATE(
+						"Error writing cache file for export \"%s\":\n%s"),
+						name, strerror(status));
+
+					_ShowAlert(alertText, B_TRANSLATE("OK"), "", "", B_STOP_ALERT);
+					return status;
+				}
+
+				status = roster->Translate(fSTXTTranslators[selected],
+					&cache,	NULL, &file, translator_out[0].type);
+				cacheEntry.Remove();
+
+				if (status != B_OK) {
+					BString alertText;
+					bs_printf(&alertText, B_TRANSLATE("Error exporting \"%s\":\n%s"), name,
+					strerror(status));
+
+					_ShowAlert(alertText, B_TRANSLATE("OK"), "", "", B_STOP_ALERT);
+					return status;
+				}
+			}
+			else 
+				status = fTextView->WriteStyledEditFile(&file);
 		}
 	}
 
@@ -821,6 +878,7 @@ StyledEditWindow::SaveAs(BMessage* message)
 		BMenuBar* menuBar = dynamic_cast<BMenuBar*>(
 			fSavePanel->Window()->FindView("MenuBar"));
 		if (menuBar != NULL) {
+			// Encoding menu
 			fSavePanelEncodingMenu = new BMenu(B_TRANSLATE("Encoding"));
 			fSavePanelEncodingMenu->SetRadioMode(true);
 			menuBar->AddItem(fSavePanelEncodingMenu);
@@ -842,6 +900,50 @@ StyledEditWindow::SaveAs(BMessage* message)
 				if (charset.GetFontID() == fTextView->GetEncoding())
 					item->SetMarked(true);
 			}
+
+			// File format menu
+			fSavePanelFormatMenu = new BMenu(B_TRANSLATE("Document format"));
+			fSavePanelFormatMenu->SetRadioMode(true);
+			menuBar->AddItem(fSavePanelFormatMenu);
+
+			BMenuItem* item = new BMenuItem(B_TRANSLATE("STXT (default)"),
+				new BMessage());
+			item->SetMarked(true);
+			fSavePanelFormatMenu->AddItem(item);
+
+			BTranslatorRoster* troster = new BTranslatorRoster;
+			troster->AddTranslators(NULL);
+			
+			int32 num_translators, num_formats;
+			translator_id* translators;
+			const char* translator_name;
+			const translation_format* translator_formats;
+
+			fSTXTTranslators.push_back(NULL);
+
+			troster->GetAllTranslators(&translators, &num_translators);
+
+			for (int32 i = 0; i<num_translators; i++) {
+				troster->GetTranslatorInfo(translators[i], &translator_name,
+					NULL, NULL);
+				troster->GetInputFormats(translators[i], &translator_formats,
+					&num_formats);
+
+				bool isSTXT = false;
+				for (int32 ix = 0; ix<num_formats; ix++) {
+					if (translator_formats[ix].type == B_TRANSLATOR_TEXT) {
+						isSTXT = true;
+						break;
+					}
+				}
+
+				if (isSTXT) {
+					fSTXTTranslators.push_back(translators[i]);
+					BMenuItem* titem = new BMenuItem(translator_name,
+						new BMessage());
+					fSavePanelFormatMenu->AddItem(titem);
+				}
+			}
 		}
 	}
 
@@ -1300,6 +1402,7 @@ StyledEditWindow::_InitWindow(uint32 encoding)
 
 	fSavePanel = NULL;
 	fSavePanelEncodingMenu = NULL;
+	fSavePanelFormatMenu = NULL;
 		// build lazily
 }
 
diff --git a/src/apps/stylededit/StyledEditWindow.h b/src/apps/stylededit/StyledEditWindow.h
index ae8aac3..d9144a6 100644
--- a/src/apps/stylededit/StyledEditWindow.h
+++ b/src/apps/stylededit/StyledEditWindow.h
@@ -15,7 +15,10 @@
 #include <Node.h>
 #include <Window.h>
 
+#include <vector>
+
 struct entry_ref;
+typedef long unsigned int translator_id;
 
 class BFilePanel;
 class BMenu;
@@ -166,6 +169,8 @@ private:
 
 			BFilePanel*			fSavePanel;
 			BMenu*				fSavePanelEncodingMenu;
+			BMenu*				fSavePanelFormatMenu;
+			vector <translator_id>	fSTXTTranslators;
 				// node monitoring
 			node_ref			fNodeRef;
 			node_ref			fFolderNodeRef;
